Stop 10 · Schema Design & ConstraintsTopics · 5/11

Auto-Increment, SERIAL and IDENTITY

Part of the SQL Roadmap

In short

Nobody wants to pick a unique ID by hand for every new order. The database can hand out the next number automatically, and each database has its own name for that feature.

Surrogate primary keys are usually generated by the database. MySQL uses AUTO_INCREMENT, PostgreSQL uses SERIAL or the standard GENERATED ... AS IDENTITY, and SQL Server uses IDENTITY.

The jaadu tears the next ticket from a thick raffle ticket book at a school fair stall, with one crumpled torn ticket lying on the table.

Think of it like this

The raffle ticket book

At a school fair in Mysuru, the raffle book has pre-printed numbers. Each buyer gets the next ticket. If a ticket tears and gets thrown away, that number is simply skipped, never reprinted. An identity column works the same way: always the next number, and gaps are perfectly normal.

Watch it happen

Identity values over time

1
0
2
1
3
2

1/5Three orders inserted. The identity handed out 1, 2 and 3.

The big ideas

What you need to know

IDENTITY is the standard

GENERATED ALWAYS AS IDENTITY is standard SQL and the recommended choice in PostgreSQL 10 and later.

SERIAL is older shorthand

SERIAL creates an integer column with its own sequence. It still works, but IDENTITY is cleaner.

ALWAYS vs BY DEFAULT

ALWAYS refuses hand-typed IDs. BY DEFAULT lets you supply one, which can clash with the sequence later.

Other dialects

MySQL uses AUTO_INCREMENT and SQL Server uses IDENTITY(1,1). The idea is the same everywhere.

See it in code

Walk through the code

sql
1CREATE TABLE orders (2  order_id      BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,3  customer_name VARCHAR(100)  NOT NULL,4  amount        NUMERIC(10,2) NOT NULL5);6 7INSERT INTO orders (customer_name, amount)8VALUES ('Meera Iyer', 1250.00),9       ('Rohit Das',   640.00)10RETURNING order_id;11 12-- Older PostgreSQL style:13-- order_id BIGSERIAL PRIMARY KEY

1/4The database generates order_id and won't accept one typed by hand.

A generated primary key in PostgreSQL. MySQL: order_id BIGINT AUTO_INCREMENT PRIMARY KEY. SQL Server: order_id BIGINT IDENTITY(1,1) PRIMARY KEY.

Save yourself the pain

Common mistakes

Oops

Expecting IDs to have no gaps and 'fixing' them.

Do this instead

Gaps from rollbacks and deletes are normal. Never renumber primary keys, because other tables point to them.

Oops

Inserting your own IDs into a BY DEFAULT or SERIAL column.

Do this instead

The sequence doesn't know about them and will later try to hand out the same number. Let the database generate IDs.

Your mission

Watch the gaps appear

~15 min

Quick check

An insert takes ID 7 but the transaction is rolled back. What ID does the next successful insert get?

Go deeper

  1. 1Create a table with a generated ID column in your database.
  2. 2Insert rows without an ID and check the generated values.
  3. 3Learn why gaps in generated IDs are normal after rollbacks and deletes.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge