CREATE SEQUENCE, ALTER SEQUENCE and NEXTVAL
MariaDB sequences: creating and changing them, NEXTVAL, LASTVAL, SETVAL and sequences as column defaults.
A sequence is a named counter that hands out numbers (MariaDB, D81). The parser accepts the statements in both dialect modes; the behaviour is verified against MariaDB 11.4 only, and MySQL has no sequences. Reading a sequence with SELECT * FROM seq works in the MariaDB dialect.
Syntax#
CREATE [OR REPLACE] SEQUENCE [IF NOT EXISTS] [db_name.]seq_name [sequence_option] ...
ALTER SEQUENCE [db_name.]seq_name [sequence_option] ...
DROP SEQUENCE [IF EXISTS] seq_name [, seq_name] ...
SHOW CREATE SEQUENCE seq_name
sequence_option:
START [WITH | =] n
| INCREMENT [BY | =] n
| MINVALUE [=] n | NO MINVALUE | NOMINVALUE
| MAXVALUE [=] n | NO MAXVALUE | NOMAXVALUE
| CACHE [=] n | NO CACHE | NOCACHE
| CYCLE | NO CYCLE | NOCYCLE
| RESTART [[WITH] n] -- ALTER SEQUENCE
| ENGINE [=] name | COMMENT [=] 'string' -- read and ignored
NEXTVAL(seq) | NEXT VALUE FOR seq
LASTVAL(seq) | PREVIOUS VALUE FOR seq
SETVAL(seq, n [, is_used])Arguments#
| Name | Meaning | Default |
|---|---|---|
START | First value. | the minimum (1 for an increasing sequence) |
INCREMENT | Step; may be negative; 0 is refused. | 1 |
MINVALUE, MAXVALUE | Bounds. START outside them, MINVALUE not below MAXVALUE, or CACHE below 1 is refused. | 1 and the largest 64-bit value minus 1 (increasing); the reverse for a negative step |
CACHE n | Values are reserved in blocks of n; the next block's start is written down. A restart loses the unused cached values, as MariaDB's does. NOCACHE is CACHE 1. | 1000 |
CYCLE | Wrap to the other bound at the end; without it NEXTVAL past the end is an error. | NOCYCLE |
RESTART [WITH n] | ALTER SEQUENCE: restart at n, or at the start value. | none |
SETVAL(seq, n, 0) | Set the next value to n (is_used 0). With 1 or omitted, n counts as used and the next value follows it. | 1 |
Remarks#
NEXTVALof a sequence that is not there is an error;LASTVALis the session's last value and is set by a statement that succeeds.- The state is shared by the sessions of the front-end.
SHOW TABLESlists sequences withTable_typeSEQUENCE;DESCRIBE seqandSELECT * FROM seqwork in the MariaDB dialect.- As a default:
DEFAULT NEXTVAL(seq)orDEFAULT (NEXT VALUE FOR seq)inCREATE TABLEgives each new row the next number. SETVALreturnsNULLwhen the new value is not ahead of the sequence's next value in the direction of its increment, and changes nothing.- Privileges:
acl.rshas no rule for the sequence statements in this version; they are not checked.
Examples#
Example:
CREATE SEQUENCE s1;
SELECT NEXTVAL(s1), NEXTVAL(s1), NEXT VALUE FOR s1; -- 1, 2, 3
SELECT LASTVAL(s1), PREVIOUS VALUE FOR s1;
CREATE SEQUENCE s2 START WITH 100 INCREMENT BY 10 MINVALUE 50 MAXVALUE 140 CACHE 3 CYCLE;
ALTER SEQUENCE s1 RESTART WITH 1000 INCREMENT BY 5;
SELECT SETVAL(s1, 50), NEXTVAL(s1);
SHOW CREATE SEQUENCE s1;
CREATE TABLE t (id INT PRIMARY KEY DEFAULT (NEXT VALUE FOR s1), v INT);
INSERT INTO t (v) VALUES (1), (2);
DROP SEQUENCE IF EXISTS nosuch;