In OceanBase Database, a sequence is an auto-increment sequence number generated in the database based on some rules. It is usually a set of numeric values (of the numeric data type). A sequence auto-increments and therefore is often used as a primary or unique key.
You can use the CREATE SEQUENCE statement to create a sequence. A sequence provides two pseudo-columns: CURRVAL and NEXTVAL, which return the current sequence value and the next sequence value respectively. Each reference to NEXTVAL will update the value of CURRVAL.
To execute this statement, you must have the CREATE privilege.
The SQL syntax is as follows:
CREATE SEQUENCE [IF NOT EXISTS] [database_name.]sequence_name
[MINVALUE int_value | NOMINVALUE]
[MAXVALUE int_value | NOMAXVALUE]
[START WITH int_value]
[INCREMENT BY int_value]
[CACHE int_value | NOCACHE]
[ORDER | NOORDER]
[CYCLE | NOCYCLE];
The parameters are described as follows:
IF NOT EXISTS: If the sequence to be created already exists andIF NOT EXISTSis not specified, the system returns an error. This parameter is optional.database_name.: the database in which the sequence resides. This parameter is optional. If you do not specifydatabase_name, the sequence is created in the current logon database by default.MINVALUE int_value | NOMINVALUE: the minimum value of the sequence. The value range is [-1027, (1027-1)].If
NOMINVALUEis specified, the minimum value is 1 for an ascending sequence and -(1027-1) for a descending sequence.If
MINVALUEis not specified,NOMINVALUEtakes effect by default.MAXVALUE int_value | NOMAXVALUE: the maximum value of the sequence. The value range is [(-1027+1), (1028-1)].If
NOMAXVALUEis specified, the maximum value is(1028-1)for an ascending sequence, and-1for a descending sequence.If
MAXVALUEis not specified,NOMAXVALUEtakes effect by default.START WITH: the start value of the sequence. The value must be less than or equal toMAXVALUE, and greater than or equal toMINVALUE. If no start value is specified, the start value is the minimum value for an ascending sequence and the maximum value for a descending sequence.INCREMENT BY: the auto-increment step of the sequence. The value cannot be0. If the value is positive, the sequence ascends. If the value is negative, the sequence descends. If this parameter is not specified, the default value1takes effect.CACHE: the number of sequence values that the database preallocates and keeps in memory. The default value is20.ORDER | NOORDER: specifies whether the values of the sequence are generated sequentially.ORDERindicates sequential generation, andNOORDERindicates non-sequential generation. The default value isNOORDER.CYCLE | NOCYCLE: specifies whether the values of the sequence are cyclically generated.CYCLEindicates a cycling sequence, andNOCYCLEindicates a non-cycling sequence. The default value isNOCYCLE.
Notice
- When both
MINVALUEandMAXVALUEare specified,MINVALUEmust be less thanMAXVALUE. - The difference between
MAXVALUEandMINVALUEmust be greater than or equal toINCREMENT BY int_value. - The value of
CACHEmust be greater than1. If the value ofCACHE INT_VALUEis1, it is equivalent toNOCACHE. - If
CYCLEis specified and the value ofINCREMENT BYis less than0,MINVALUEmust be specified. - If
CYCLEis specified, the number of values in the cache must be less than the number of values in the cycle.
The following example creates the sequence seq1 and specify the auto-increment step to 2:
obclient> CREATE SEQUENCE seq1 START WITH 1 MINVALUE 1 MAXVALUE 10 INCREMENT BY 2 NOCYCLE NOORDER CACHE 30;
For more information about the CREATE SEQUENCE statement, see CREATE SEQUENCE.
References
For more information about operations on a sequence, see the following topics: