CREATE SEQUENCE seqname [ INCREMENT increment ] [ MINVALUE minvalue ] [ MAXVALUE maxvalue ] [ START start ] [ CACHE cache ] [ CYCLE ]
The name of the new sequence.
The value to be applied on each sequence increment. Specify a positive number to make an ascending sequence; specify a negative number to make a descending sequence.
The minimum value the new sequence will generate. The default minimum is 1 for an ascending sequence and –2147483647 for a descending sequence.
The maximum value the new sequence will generate. The default is 2147483647 for an ascending sequence, and –1 for a descending sequence.
The starting value of the sequence. By default, an ascending sequence will start at minvalue, and a descending sequence will start at maxvalue.
The quantity of sequence numbers that can be stored in cache memory. Using a cache value greater than 1 will speed up performance, because some calls for new sequence values will be satisfied from the cache. By default, the cache value is set at 1, which forces generation of one sequence number at a time (by default, cache is not used). Set it to a number higher than 1 to enable the use of caching.
Use this keyword to enable wrapping. When wrapping is enabled, a sequence can wrap around past its minimum or maximum value and begin again at its minimum or maximum value. The direction of the wrap depends on whether a sequence is ascending or descending.
The message returned when a sequence is created successfully.
The error returned if the sequence already exists.
The error returned if the sequence's minimum starting value is out of range.
The error returned if the starting value is out of range.
The error returned if the minimum and maximum values are incompatible.
Use the CREATE SEQUENCE command to create a new sequence number generator into the database.
This example demonstrates the creation of a sequence named shipments_ship_id_seq:
booktown=# CREATE SEQUENCE shipments_ship_id_seq booktown-# START 200 INCREMENT 1; CREATE
Once created, you can select the next number from a sequence with the nextval() function:
booktown=# SELECT nextval ('shipments_ship_id_seq'); nextval --------- 200 (1 row)
You can also use a sequence in an INSERT command:
booktown=# INSERT INTO shipments VALUES booktown-# (nextval('shipments_ship_id_seq'), 107, '0394800753', 'now');