Design and best practice · Designing databases

Auto-generated keys: IDENTITY, AUTO_INCREMENT and AUTOINCREMENT

An auto-generated column lets the engine assign an id when a row is inserted. Declare the matching uniqueness rule as well.

Before continuing: This entry is part of the constraints and keys guide. If you are new to the topic, start with CREATE TABLE.

Example

In an ordinary SQLite table, a column declared exactly as INTEGER PRIMARY KEY is an alias for ROWID. If you omit its value in a INSERT, SQLite normally assigns an available integer automatically.

SQLite 3.46.1 · verified
CREATE TEMP TABLE customers_demo (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);

INSERT INTO customers_demo (name)
VALUES ('Amy'), ('Liam'), ('Megan');

SELECT id, name
FROM customers_demo
ORDER BY id;

SQLite also accepts the keyword AUTOINCREMENT, but its documentation warns that it adds overhead and is usually unnecessary. Its main purpose is to change the policy for reusing ROWIDvalues, not to “switch on” the basic generation of INTEGER PRIMARY KEY.

How it is used

Picture a customers table. Your application knows the name and the email address, but it does not need to decide what the next idwill be. When the row is inserted, the database generates the id using that engine's mechanism.

The identity column does not appear in the INSERT. The engine decides which value it gets.

Differences and trade-offs

A generated column computes its value from other columns in the row, for example price * quantity. An identity column produces an independent value, usually to identify new rows. In PostgreSQL the two are separate features, and one column cannot be both an identity and a generated column.

Avoid this confusion

Assuming the numbers will never have gaps

An identity column generates values; it does not promise a perfect, gap-free sequence. Rollbacks, internal caching, deletes and engine rules can all leave gaps. Do not use the id as a business counter.

Copying another engine's syntax

AUTO_INCREMENT, IDENTITY and AUTOINCREMENT are not syntactic synonyms. Check the engine documentation before designing a migration.

Confusing value generation with integrity

If the column must identify each row uniquely, declare the matching constraint. In SQL Server, for example, the IDENTITY property does not guarantee uniqueness on its own.

Key differences between engines
Common ways to declare an auto-generated key
EngineExampleKey point
PostgreSQLid BIGINT GENERATED ALWAYS AS IDENTITYThe identity column uses an implicit sequence. ALWAYS and BY DEFAULT control how explicit values are treated.
MySQLid BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEYAUTO_INCREMENT generates values; the primary key is still declared as a rule of its own.
SQLiteid INTEGER PRIMARY KEYis an alias for ROWID in ordinary tables. AUTOINCREMENT changes how values are reused and is usually unnecessary.
SQL Serverid BIGINT IDENTITY(1,1) PRIMARY KEYIDENTITY generates values from a seed and an increment, but on its own it does not replace a uniqueness constraint.