A sequence is an auto-increment sequence generated by the database based on predefined rules. A sequence is often used as the primary key or unique key. This topic describes the value setting method, application scenarios, and usage notes of sequences.
Reference a sequence
You can use the following pseudocolumns to reference sequence values in SQL statements:
CURRVAL: returns the current value of the sequence.NEXTVAL: returns the next incremental value.
To use a sequence pseudocolumn, you must qualify CURRVAL and NEXTVAL with the name of the sequence followed by a period (.). For example, if the name of the sequence is SEQ_FOO, you can use SEQ_FOO.CURRVAL to query the current value of the SEQ_FOO sequence. Likewise, you can use SEQ_FOO.NEXTVAL to query the next value of the SEQ_FOO sequence.
Application scenarios
Sequence values referenced by using CURRVAL and NEXTVAL can be used in the following positions:
The select list of a
SELECTstatement that is not contained in a subquery or view.The select list of a subquery in an
INSERTstatement.The
VALUESclause of anINSERTstatement.The
SETclause of anUPDATEstatement.
Values referenced by using CURRVAL and NEXTVAL cannot be used in the following positions:
A subquery in a
DELETE,SELECT, orUPDATEstatement.A query for a view.
A
SELECTstatement with aDISTINCToperator.A
SELECTstatement with aGROUP BYclause or anORDER BYclause.A
SELECTstatement that is combined with anotherSELECTstatement by using theUNION,INTERSECT, orMINUSset operator.The
WHEREclause of aSELECTstatement.The
DEFAULTvalue of a column in aCREATE TABLEorALTER TABLEstatement.The condition of a
CHECKconstraint.
Considerations
When you create a sequence, you need to define its initial value and increment. The first reference to NEXTVAL returns the initial value of the sequence. Subsequent references to NEXTVAL increment the sequence value by the defined increment and return a new value. Any reference to CURRVAL always returns the current value of the sequence, which is the value returned by the last reference to NEXTVAL.
Before you refer to CURRVAL of a sequence in the session, you must use NEXTVAL to initialize the sequence values first.
When you create a sequence, you can define its initial value and the increment between two values. The first reference to NEXTVAL returns the initial value of the sequence. Subsequent references to NEXTVAL increment the sequence value by the defined increment and return a new value. Any reference to CURRVAL always returns the current value of the sequence, which is the value returned by the last reference to NEXTVAL. For more information about how to create a sequence, see CREATE SEQUENCE.
When you reference NEXTVAL in a single SQL statement, OceanBase Database increments the sequence in the following way:
It increments once for each row returned by the outer query block of a
SELECTstatement. Such a query block can appear in the following positions:- A top-level
SELECTstatement. - An
INSERT... SELECTstatement. For a multi-table insert, you must referenceNEXTVALin theVALUESclause, and the sequence is incremented once for each row returned by the subquery, even if you referenceNEXTVALin multiple branches of the insert. - A
CREATE TABLE ... AS SELECTstatement. - A
CREATE MATERIALIZED VIEW ... AS SELECTstatement.
- A top-level
The sequence is incremented once for each row updated in an
UPDATEstatement.The sequence is incremented for each
INSERTstatement containing aVALUESclause.The sequence is incremented for each row merged by a
MERGEstatement.NEXTVALcan be referenced in either or both of themerge_insert_clauseormerge_update_clauseclause. TheNEXTVALvalue is incremented for each row updated or inserted, even if the sequence number is not used in the update or insert operation. IfNEXTVALis referenced more than once at these positions, the sequence is incremented once for each row and returns the same value for all appearances ofNEXTVALin that row.
When NEXTVAL is referenced more than once in these positions, the sequence is incremented once and all references to NEXTVAL return the next value of the current sequence.
When CURRVAL and NEXTVAL are both referenced more than once in these positions, OceanBase Database increments the sequence, and all references to CURRVAL and NEXTVAL return the next value of the current sequence.
A sequence can be accessed by many users at the same time without waiting or locking. The following are sample statements in which a single sequence is queried:
SELECT SEQUENCE_NAME.NEXTVAL FROM DUAL; /*The sequence number increments each time the statement is executed.*/
SELECT SEQUENCE_NAME.CURRVAL FROM DUAL; /*The sequence remains the same no matter how many times this statement is executed.*/