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 at the same time, 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 creating a sequence, you can use the CURRVAL pseudocolumn to return the current sequence value or use the NEXTVAL pseudocolumn to return the new increment value in the SQL statement. For more information, see Sequence pseudocolumns.
Syntax
CREATE [WITH HINT] SEQUENCE [ schema. ] sequence_name
[ INCREMENT BY simple_num ]
[ START WITH simple_num ]
[ MAXVALUE simple_num | NOMAXVALUE ]
[ MINVALUE simple_num | NOMINVALUE ]
[ CYCLE | NOCYCLE ]
[ CACHE simple_num | NOCACHE ]
[ ORDER | NOORDER ]
[ RESTART ]
Parameters
| Field | Description |
|---|---|
| WITH HINT | Optional. Specifies the hints used by the query optimizer. |
| 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. |
| INCREMENT BY simple_num | Specifies the increment step for the sequence. simple_num can be a positive or negative number, but cannot be 0. If you specify a positive number, the sequence is in ascending order; if you specify a negative number, the sequence is in descending order. The default value is 1 if not specified. |
| START WITH simple_num | Specifies the starting value of the sequence. simple_num 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 order and the maximum value for descending order. |
| MAXVALUE simple_num | Specifies the maximum value of the sequence. The value of simple_num ranges from [-9223372036854775808, 9223372036854775807].Notice
|
| NOMAXVALUE | The default value. If you specify NOMAXVALUE, the maximum value is 9223372036854775807 for ascending order and -1 for descending order. |
| MINVALUE simple_num | Specifies the minimum value of the sequence. The value of simple_num ranges from [-9223372036854775808, 9223372036854775807]. |
| NOMINVALUE | The default value. If you specify NOMINVALUE, the minimum value is 1 for ascending order and -9223372036854775808 for descending order. |
| CYCLE | Specifies that the sequence generates values in a cycle. That is, the sequence continues to generate values after reaching its maximum or minimum value. For ascending order, it generates the minimum value after reaching the maximum value. For descending order, it generates the maximum value after reaching the minimum value. Notice
|
| NOCYCLE | The default value. Specifies that the sequence cannot generate more values after reaching its maximum or minimum value. |
| CACHE simple_num | Specifies the number of incremented values to preallocate in memory. The default value is 20. The value of CACHE simple_num must be greater than 1. If the value of CACHE simple_num 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 generates values in order. |
| NOORDER | The default value. Specifies that the sequence does not generate values in order. |
| RESTART | Specifies that the sequence starts from the minimum value. If you specify CYCLE, the sequence automatically restarts after reaching the maximum value. |
Examples
Create a sequence named seq1 in the test schema and specify an increment step of 2. In the following example, the first reference to seq1.nextval returns 1, and the second reference returns 3. 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