You can use the CREATE SEQUENCE statement to create a sequence. This topic describes how to create a sequence.
Syntax
CREATE SEQUENCE [ schema. ] sequence_name
{ START WITH int_value
|[ INCREMENT BY int_value ]
|[ MINVALUE int_value | NOMINVALUE ]
|[ MAXVALUE int_value | NOMAXVALUE ]
|[ CACHE int_value | NOCACHE ]
|[ ORDER | NOORDER ]
|[ CYCLE | NOCYCLE ]
};
Parameters
| Parameter | Description |
|---|---|
| schema. | The schema where the sequence to be created resides. If this parameter is not specified, the database creates the sequence in the schema of the current user. SET GLOBALSET GLOBAL |
| sequence_name | The name of the sequence to be created. SET GLOBALSET GLOBAL |
| MINVALUE int_value | The minimum value of the sequence. The value range of int_value is [-9223372036854775808,9223372036854775807]. SET GLOBALSET GLOBAL |
| NOMINVALUE | NOMINVALUE is the default value. If NOMINVALUE is specified, the minimum value is 1 when the sequence ascends, and is -9223372036854775808 when the sequence descends. SET GLOBALSET GLOBAL |
| MAXVALUE int_value | The maximum value of the sequence. The value range of int_value is [-9223372036854775808,9223372036854775807].
Notice |
| NOMAXVALUE | NOMAXVALUE is the default value. If NOMAXVALUE is specified, the maximum value is 9223372036854775807 when the sequence ascends or -1 when the sequence descends. SET GLOBALSET GLOBAL |
| START WITH int_value | The start value of the sequence. int_value must be smaller than or equal to MAXVALUE, and greater than or equal to MINVALUE. 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. SET GLOBAL |
| INCREMENT BY int_value | The increment step of the sequence. int_value cannot be 0. If the value is positive, the sequence ascends. If the value is negative, the sequence descends. If this parameter is not specified, the default value is 1. SET GLOBALSET GLOBAL |
| CACHE int_value | The number of sequence values pre-allocated in the memory. By default, int_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. SET GLOBAL |
| NOCACHE | Specifies that no sequence values are pre-allocated. If neither CACHE nor NOCACHE is specified, the database caches 20 sequence values by default. SET GLOBALSET GLOBAL |
| ORDER | Specifies that sequence values are generated in order. SET GLOBALSET GLOBAL |
| NOORDER | NOORDER is the default value, which means it is not assured that sequence values are generated in order. SET GLOBALSET GLOBAL |
| CYCLE | Specifies that sequence values are generated cyclically. That is, when the sequence reaches its maximum or minimum value, new sequence values will also be generated. If the sequence ascends and reaches its maximum value, the minimum sequence value will be generated. If the sequence descends and reaches its minimum value, the maximum sequence value will be generated.
Notice |
| NOCYCLE | NOCYCLE is the default value. If it is specified, no more values are generated when the sequence reaches its maximum or minimum value. SET GLOBALSET GLOBAL |
Examples
Create a sequence named seq1 and specify the increment step to 2. In the following example, 1 is returned for the first reference to seq1.nextval, and 2 is returned for the second reference. The value returned for each subsequent reference increments by 1.
Create a sequence named
seq1.obclient> CREATE SEQUENCE seq1 MINVALUE 1 MAXVALUE 5 NOCYCLE NOORDER NOCACHE; Query OK, 0 rows affectedNote
You can view the sequence that you have created in the
USER_SEQUENCES,ALL_SEQUENCES, andDBA_SEQUENCESviews.Initiate the first reference to
seq1.nextval.obclient> SELECT seq1.nextval FROM DUAL; +---------+ | NEXTVAL | +---------+ | 1 | +---------+ 1 row in setInitiate the second reference.
obclient> SELECT seq1.nextval FROM DUAL; +---------+ | nextval | +---------+ | 2 | +---------+ 1 row in set