CREATE SEQUENCE: standalone number generators
A sequence is a number generator that belongs to the schema, not to one column: several tables can draw from it, and application code can too. It is the standard answer to 'I need IDs without a row' and it is missing from two of the five engines.
- Family
- UUIDs & identifiers
- Missing in
- MySQL, SQLite
- Gotchas
- 5 listed
Engine by engine
Sequences are objects you CREATE and pull from with NEXTVAL/NEXT VALUE FOR. PostgreSQL, Oracle and SQL Server have them; MySQL and SQLite substitute per-table counters.
| Движок | Тип | Хранилище | Заметки |
|---|---|---|---|
| SQL Server |
CREATE SEQUENCE
|
8 bytes (BIGINT by default) | Since 2012; value fetched with NEXT VALUE FOR seq. Can be shared by tables and used as a DEFAULT, but is not tied to a column's lifecycle. |
| PostgreSQL |
CREATE SEQUENCE
|
8 bytes | First-class object: nextval('seq'), currval, setval; OWNED BY a column ties its lifecycle to a table. Every identity column backs onto one. |
| MySQL |
none (AUTO_INCREMENT)
Workaround
|
н/д | No standalone sequence object (MariaDB has one, MySQL does not). Workarounds: a counter table bumped with LAST_INSERT_ID(expr), or per-table AUTO_INCREMENT. |
| SQLite |
none (rowid)
Workaround
|
н/д | No sequences. INTEGER PRIMARY KEY assigns rowids; the hidden sqlite_sequence table only tracks AUTOINCREMENT counters. |
| Oracle |
CREATE SEQUENCE
|
NUMBER (variable) | The classic: seq.NEXTVAL / seq.CURRVAL, CACHE and NOCACHE options; pre-12c this plus a trigger was the standard identity column emulation. |
Gotchas
Where the engines disagree. These tend to surface as bugs during a migration or a port.
- There is no portable way to read the next value: PostgreSQL uses nextval('seq'), SQL Server NEXT VALUE FOR seq, Oracle seq.NEXTVAL. Migrations rewrite all three.
- MySQL's common emulation, UPDATE counters SET id = LAST_INSERT_ID(id + 1) followed by SELECT LAST_INSERT_ID(), serializes on that row: every caller waits for the previous transaction to commit.
- Sequences never roll back. Oracle's CACHE option precomputes a batch and discards it on instance restart, so gaps can be much larger than one.
- A SQL Server sequence can be a table's DEFAULT, but nothing stops two tables sharing it; identities are per column, sequences are free-floating, and that is the design decision to make consciously.
- In PostgreSQL, nextval() and setval() are never rolled back: setval('s', 500) inside a rolled-back transaction still leaves the sequence at 500. ALTER SEQUENCE ... RESTART, by contrast, does roll back.
Common questions
When do I need a sequence instead of an identity column?
When IDs are shared across tables, generated before the insert (child rows that reference a parent key you do not have yet), or handed to application code directly. A per-table counter does not do any of that.
How do I get sequence-like behavior in MySQL or SQLite?
Neither has a sequence object. MySQL's counter-table trick works but is a hotspot; SQLite only has rowids. If cross-table ids matter, generate them in the application (snowflake ids, ULIDs, UUIDv7) instead.
Do sequences create gaps?
Always, eventually: values are handed out and never returned, cached batches are dropped on crash, and transactions that roll back keep the number. Plan keys for uniqueness, not for contiguity.
Related on SQLly
Map a type to the other four engines
The free type converter maps a type from one engine to its closest equivalent in the others and says where storage, precision or behaviour differ. The desktop app does the same mapping while you edit.