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 a sequence is created, you can use the CURRVAL pseudocolumn in an SQL statement to return the current value of the sequence or use the NEXTVAL pseudocolumn to return the incremented new value. 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
| Parameter | 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 specified as a positive number, the sequence is in ascending order; if specified as a negative number, the sequence is in descending order. The default value is 1 when 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 sequences and the maximum value for descending sequences. |
| MAXVALUE simple_num | Specifies the maximum value of the sequence. The range of simple_num is [-9223372036854775808, 9223372036854775807].Notice
|
| NOMAXVALUE | The default value. If specified as NOMAXVALUE, the maximum value for ascending sequences is 9223372036854775807, and for descending sequences is -1. |
| MINVALUE simple_num | Specifies the minimum value of the sequence. The range of simple_num is [-9223372036854775808, 9223372036854775807]. |
| NOMINVALUE | The default value. If specified as NOMINVALUE, the minimum value for ascending sequences is 1, and for descending sequences is -9223372036854775808. |
| 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. For ascending sequences, it generates the minimum value after reaching the maximum value. For descending sequences, 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 be preallocated 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 both CACHE and NOCACHE are omitted, 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. |
| RESTART | Specifies that the sequence starts from the minimum value. If CYCLE is specified, the sequence automatically restarts after reaching the maximum value. |
Examples
Create a sequence named seq1 in the test schema with 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
