Sequences
Independent database objects that generate unique numeric values, separate from any specific table.
sequence CREATE SEQUENCE NEXTVAL sequence generator serial
sequence auto-increment identity serial generator
| Database | Status | Syntax Family | Min Version |
|---|---|---|---|
| PostgreSQL | full | - | - |
| MySQL | none | - | - |
| MariaDB | full | - | 10.3 |
| Oracle | full | - | - |
| SQL Server | full | - | 2012 |
| SQLite | none | - | - |
| Redshift | none | - | - |
| DB2 | full | - | - |
| Snowflake | full | - | - |
| BigQuery | none | - | - |
Compare Databases
Database Details
PostgreSQL
full
CREATE SEQUENCE order_seq
START WITH 1000
INCREMENT BY 1
NO CYCLE;
-- Advance and return next value
SELECT NEXTVAL('order_seq');
-- Return current value (within session)
SELECT CURRVAL('order_seq');
-- Reset sequence to a specific value
SELECT SETVAL('order_seq', 5000);- SERIAL / BIGSERIAL auto-create a sequence behind the column. For new code, prefer GENERATED AS IDENTITY (10+).
- CACHE n preallocates n values per session to reduce cross-session contention, at the cost of larger gaps if a session exits.
MySQL
none
- Not supported. Auto-increment lives on the column via AUTO_INCREMENT.
- A single-row counter table plus SELECT LAST_INSERT_ID(new_val) is the common substitute for a standalone counter.
Docs: AUTO_INCREMENT
MariaDB
full
CREATE SEQUENCE order_seq
START WITH 1000
INCREMENT BY 1
CACHE 50;
-- Advance and return next value
SELECT NEXTVAL(order_seq);
-- Return last used value in session
SELECT LASTVAL(order_seq);
-- Reset sequence
SELECT SETVAL(order_seq, 5000);- Added in 10.3. Syntax is broadly compatible with Oracle and PostgreSQL.
- Can be used as a column default: col INT DEFAULT NEXTVAL(seq).
Docs: CREATE SEQUENCE, Sequence Overview
Oracle
full
CREATE SEQUENCE order_seq
START WITH 1000
INCREMENT BY 1
CACHE 20
NOCYCLE;
-- Get next value
SELECT order_seq.NEXTVAL FROM DUAL;
-- Get current value (within session)
SELECT order_seq.CURRVAL FROM DUAL;- CACHE preallocates values in SGA memory to cut contention on hot sequences; rollbacks and instance failures can leave gaps.
- 12c+ allows sequence.NEXTVAL directly as a column DEFAULT — no more FROM DUAL workaround.
Docs: CREATE SEQUENCE
SQL Server
full
CREATE SEQUENCE dbo.order_seq
AS INT
START WITH 1000
INCREMENT BY 1
CACHE 50
NO CYCLE;
-- Get next value
SELECT NEXT VALUE FOR dbo.order_seq;
-- Batch allocation
EXEC sp_sequence_get_range
@sequence_name = N'dbo.order_seq',
@range_size = 100,
@range_first_value = @first_value OUTPUT;- Added in 2012. NEXT VALUE FOR can appear in DEFAULT, INSERT, and SELECT; no CURRVAL.
- sp_sequence_get_range reserves a batch of values in one call.
Docs: CREATE SEQUENCE, NEXT VALUE FOR
SQLite
none
- Not supported. INTEGER PRIMARY KEY aliases the rowid and auto-increments; AUTOINCREMENT additionally prevents rowid reuse.
Docs: SQLite Autoincrement
Redshift
none
- No sequence object despite the PostgreSQL lineage. Use IDENTITY or GENERATED BY DEFAULT AS IDENTITY instead.
DB2
full
CREATE SEQUENCE order_seq
AS INTEGER
START WITH 1000
INCREMENT BY 1
CACHE 20
NO CYCLE;
-- Get next value
VALUES NEXTVAL FOR order_seq;
-- Get previous value (within session)
VALUES PREVVAL FOR order_seq;- NEXTVAL FOR / PREVVAL FOR is the DB2 spelling for NEXTVAL / CURRVAL. Usable in DEFAULT and INSERT.
Docs: CREATE SEQUENCE
Snowflake
full
CREATE SEQUENCE order_seq
START = 1000
INCREMENT = 1;
-- Use in a query
SELECT order_seq.NEXTVAL;- Parameters are START = n and INCREMENT = n, not START WITH / INCREMENT BY. NEXTVAL only; no CURRVAL.
- Values aren't contiguous under concurrency — expect gaps.
Docs: CREATE SEQUENCE
BigQuery
none
- Not supported. GENERATE_UUID() covers unique string ids; otherwise, generate ids upstream.
Docs: GENERATE_UUID function