// data type reference

Auto increment and identity columns

The cheapest primary key is a counter the engine hands out on insert. Every engine has one; every engine spells it differently, and the fine print (gaps, explicit values, one-per-table rules) differs more than the syntax suggests.

Missing in
None of the five
Gotchas
5 listed

Engine by engine

All five engines can auto-number a column. The standard spelling is GENERATED ... AS IDENTITY; MySQL keeps AUTO_INCREMENT, SQLite uses INTEGER PRIMARY KEY, and gaps are normal everywhere.

Auto increment & identity in SQL Server, PostgreSQL, MySQL, SQLite and Oracle: type name, storage and notes
Engine Typ Speicher Notizen
SQL Server IDENTITY(1,1) On the host column (4 or 8 bytes as INT/BIGINT) Declared per column; insert explicit values only after SET IDENTITY_INSERT table ON, session-scoped and table-bound.
PostgreSQL GENERATED AS IDENTITY On the host column Since PostgreSQL 10; owns its backing sequence. GENERATED ALWAYS rejects explicit values unless the INSERT says OVERRIDING SYSTEM VALUE; BY DEFAULT accepts them. SERIAL is the older shorthand.
MySQL AUTO_INCREMENT On the host column At most one per table, and it must be the first column of some index. Integer columns in practice; FLOAT/DOUBLE support is deprecated.
SQLite INTEGER PRIMARY KEY 1-8 bytes (varint rowid) Any INTEGER PRIMARY KEY is an alias for the rowid and auto-assigns; the AUTOINCREMENT keyword is stricter (never reuses ids) and slower, rarely needed.
Oracle GENERATED AS IDENTITY On the host column Since 12c, one identity column per table; before that, a SEQUENCE plus a BEFORE INSERT trigger was the house pattern, and plenty of those still exist.

Gotchas

Where the engines disagree. These tend to surface as bugs during a migration or a port.

  1. Gaps are not a bug: a rolled-back insert burns the number in SQL Server, PostgreSQL, MySQL and Oracle (SQLite reuses it). TRUNCATE resets the counter in SQL Server and MySQL, but PostgreSQL keeps counting unless you write TRUNCATE ... RESTART IDENTITY.
  2. SQL Server, MySQL and Oracle allow one auto-numbered column per table (Oracle raises ORA-30669 for a second); PostgreSQL accepts several identity columns in one table.
  3. SQLite's AUTOINCREMENT keyword is not needed to get auto-numbering: a plain INTEGER PRIMARY KEY already does it. AUTOINCREMENT only forbids id reuse after deletes, at the cost of a sqlite_sequence lookup per insert.
  4. Inserting explicit values behaves differently: SQL Server needs IDENTITY_INSERT toggled on, PostgreSQL identity columns refuse it by default, MySQL accepts it freely and bumps the counter.
  5. An INT identity stops at 2,147,483,647 values, not rows: gaps from rollbacks, deletes and SQL Server's identity cache all spend numbers. High-churn tables (sessions, logs) should start at BIGINT.

Common questions

Why do my identity values skip numbers?

Rollbacks and failed inserts still consume values; SQL Server also caches ranges and throws away unused ones on restart. If a gap is unacceptable (order numbers, invoices), generate them in a transaction, not with a counter.

Is SERIAL deprecated in PostgreSQL?

Not deprecated, but superseded. Identity columns are standard SQL, need no separate GRANT on the backing sequence, and in the ALWAYS form reject accidental explicit values.

How do I copy rows into a table with an IDENTITY column?

Either toggle SET IDENTITY_INSERT on (SQL Server), use OVERRIDING SYSTEM VALUE (PostgreSQL), or just omit the key column and let new numbers be generated; the last option is usually what you want anyway.

// map types before you move data

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.