// dialect comparison

Insert an explicit id into an identity column

Migrating data with its original ids: three engines make you ask first, two just accept the value.

Filed under Inserting & updating

The verdict

MySQL and SQLite are the easy ones. In SQL Server the switch is per session and per table; in PostgreSQL the override clause is only needed for GENERATED ALWAYS; in Oracle a GENERATED ALWAYS column has to be altered to BY DEFAULT for the load. SQL Server, MySQL and SQLite move the counter for you, PostgreSQL and Oracle don't.

Where they differ

Identity columns are meant to be numbered by the engine, but seed data and migrations need the original ids. SQL Server gates it behind a session switch, PostgreSQL needs OVERRIDING SYSTEM VALUE for GENERATED ALWAYS columns, Oracle refuses explicit values for GENERATED ALWAYS outright, and MySQL and SQLite accept them.

How each engine writes it

SQL Server

SET IDENTITY_INSERT customers ON;
INSERT INTO customers (id, email) VALUES (7, '[email protected]');
SET IDENTITY_INSERT customers OFF;

One table per session at a time, and it needs ownership or ALTER permission on the table. If the explicit id is above the current identity value, the counter moves up to it.

PostgreSQL

INSERT INTO customers (id, email)
OVERRIDING SYSTEM VALUE VALUES (7, '[email protected]');

Required for GENERATED ALWAYS; GENERATED BY DEFAULT and serial columns take explicit ids without it. The sequence does not move, so call setval() afterwards or the next generated id collides.

MySQL

INSERT INTO customers (id, email) VALUES (7, '[email protected]');

Just insert it. The AUTO_INCREMENT counter moves past the largest value inserted, so numbering continues safely.

SQLite

INSERT INTO customers (id, email) VALUES (7, '[email protected]');

Allowed even with AUTOINCREMENT; the sqlite_sequence counter jumps up if the explicit id is larger.

Oracle

ALTER TABLE customers MODIFY id GENERATED BY DEFAULT AS IDENTITY;
INSERT INTO customers (id, email) VALUES (7, '[email protected]');
ALTER TABLE customers MODIFY id GENERATED ALWAYS AS IDENTITY (START WITH LIMIT VALUE);

Oracle has no OVERRIDING SYSTEM VALUE: an explicit id in a GENERATED ALWAYS column fails with ORA-32795. Switch the column to BY DEFAULT for the load, then back; START WITH LIMIT VALUE moves the counter past the highest id.

Questions that come up

Why is IDENTITY_INSERT limited to one table?

That is a documented restriction: turning it on for a second table while it is on for another fails with an error naming the first table. Turn it on and off around each table's load.

How do I fix the counter after a migration?

SQL Server: DBCC CHECKIDENT ('customers', RESEED) if you need to change it. PostgreSQL: SELECT setval(pg_get_serial_sequence('customers', 'id'), max(id)) FROM customers. MySQL and SQLite adjust on their own. Oracle: ALTER TABLE customers MODIFY id GENERATED ALWAYS AS IDENTITY (START WITH LIMIT VALUE).

Should I use GENERATED ALWAYS or BY DEFAULT?

ALWAYS blocks accidental explicit ids from application code, at the cost of an extra step in migrations: OVERRIDING SYSTEM VALUE in PostgreSQL, an ALTER in Oracle. BY DEFAULT is more convenient for tables that are often seeded with fixed ids.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.