Purpose
This statement is used to create a sequence. A sequence is a database object that generates unique integers.
If two users increment the same sequence simultaneously, each user may receive a sequence number with a gap, and one user will never receive the sequence number generated by the other user. Sequence numbers are generated independently of tables, so the same sequence can be used for one or more tables.
After a sequence is created, you can use the CURRVAL pseudocolumn to return the current value of the sequence or the NEXTVAL pseudocolumn to return the next incremented value in an SQL statement. For more information, see Sequence pseudocolumns.
Syntax
CREATE SEQUENCE [ IF NOT EXISTS ] [ schema. ] sequence_name
[ INCREMENT BY int_value ]
[ START WITH int_value ]
[ MAXVALUE int_value | NOMAXVALUE ]
[ MINVALUE int_value | NOMINVALUE ]
[ CYCLE | NOCYCLE ]
[ CACHE int_value | NOCACHE ]
[ ORDER | NOORDER ]
Parameters
| Field | Description |
|---|---|
| schema. | Specifies the schema that contains the sequence. If you omit the schema, the database creates the sequence in the current schema. |
| sequence_name | The name of the sequence to create. |
| IF NOT EXISTS | If the specified sequence name already exists and you do not specify IF NOT EXISTS, an error is returned. |
| INCREMENT BY int_value | Specifies the increment step for the sequence. int_value cannot be 0. If you specify a positive number, the sequence is ascending; if you specify a negative number, the sequence is descending. The default value is 1 if not specified. |
| START WITH int_value | Specifies the starting value of the sequence. int_value must be less than or equal to MAXVALUE and greater than or equal to MINVALUE. If not specified, the default value is the minimum value for ascending sequences and the maximum value for descending sequences. |
| MAXVALUE int_value | Specifies the maximum value of the sequence. The range of int_value is [(-1027+1) ~ (1028-1)]. Notice
|
| NOMAXVALUE | The default value. If you specify NOMAXVALUE, the maximum value is (1028-1) for ascending sequences and -1 for descending sequences. |
| MINVALUE int_value | Specifies the minimum value of the sequence. The range of int_value is [-(1027-1) ~ (1027-1)]. |
| NOMINVALUE | The default value. If you specify NOMINVALUE, the minimum value is 1 for ascending sequences and -(1027-1) for descending sequences. |
| CYCLE | Specifies that the sequence values are generated in a cycle. That is, the sequence continues to generate values after reaching its maximum or minimum value. When the sequence reaches its maximum value in ascending order, it generates the minimum value. When the sequence reaches its minimum value in descending order, it generates the maximum value. Notice
|
| NOCYCLE | The default value. Specifies that the sequence cannot generate more values after reaching its maximum or minimum value. |
| CACHE int_value | Specifies the number of incremented values to be preallocated in memory. The default value is 20. The value of CACHE int_value must be greater than 1. If the value of CACHE INT_VALUE is 1, it is equivalent to NOCACHE. |
| NOCACHE | Specifies that the sequence values are not preallocated. If you omit both CACHE and NOCACHE, the database caches 20 sequence numbers by default. |
| ORDER | Specifies that the sequence values are generated in order. |
| NOORDER | The default value. Specifies that the sequence values are not guaranteed to be generated in order. |
Examples
Create a sequence seq1 in the test schema and specify an increment step of 2. In the following example, the first reference to seq1.nextval returns 1, the second reference returns 3, and each subsequent reference returns a value that is 2 greater than the previous one.
obclient> CREATE SEQUENCE test.seq1 START WITH 1 MINVALUE 1 MAXVALUE 10 INCREMENT BY 2 NOCYCLE NOORDER CACHE 30;
obclient> SELECT seq1.nextval FROM DUAL;
Expected return result:
+--------+
| nextval|
+--------+
| 1 |
+--------+
1 row in set
obclient> SELECT seq1.nextval FROM DUAL;
Expected return result:
+--------+
| nextval|
+--------+
| 3 |
+--------+
1 row in set
