Skip to main content

NEXTVAL(SEQ) function

The NEXTVAL(SEQ) function is a sequence function that retrieves the next value of a specified sequence and increments the sequence.

Syntax

The following example shows how to use the NEXTVAL function.

Create a sequence

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 a sequence

  • Retrieve the value of a sequence

    SELECT NEXTVAL(my_seq);
    +---------+
    | NEXTVAL |
    +---------+
    | 1 |
    +---------+
    1 row in set (0.001 sec) (0.015 sec)
  • Use a sequence when inserting data

    INSERT INTO users (id, name) VALUES (NEXTVAL(my_seq), 'John');