The NEXTVAL(SEQ) function is a sequence function that retrieves the next value of a specified sequence and increments the sequence.
Examples
To use NEXTVAL, you need to create a sequence first. The following example shows how to create a sequence and use NEXTVAL.
Create a sequence
obclient> CREATE SEQUENCE my_seq
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 999999;
where:
START: specifies the starting value.INCREMENT: specifies the step size.MINVALUE: specifies the minimum value.MAXVALUE: specifies the maximum value.
Use the sequence
Retrieve the sequence value.
obclient> SELECT NEXTVAL(my_seq); +---------+ | NEXTVAL | +---------+ | 1 | +---------+ 1 row in set (0.015 sec)Use the sequence when inserting data.
obclient> INSERT INTO users (id, name) VALUES (NEXTVAL(my_seq), 'John');