Surrogate vs Natural vs UUID Keys in SQL
Meaningless-and-stable wins PK; meaningful gets UNIQUE
Surrogate integers (IDENTITY): compact, fast, meaningless — the default. Natural keys (email, SSN): meaningful but mutable — UNIQUE, never PK. UUIDs: globally unique, merge-safe, larger with random-IO cost (v7/ordered mitigates). Junctions and business uniqueness ride composite/UNIQUE; PK stays stable and narrow for FK fan-out.
Surrogate default
BIGINT IDENTITY — compact, fast, meaningless. Narrow FK fan-out.
UUID for merge
gen_random_uuid / v7 — globally unique, larger, random-IO heavier (v7 mitigates).
Natural as UNIQUE
Email/SSN meaningful but mutable — UNIQUE + NOT NULL, never PK.
Hybrid pragmatic
Surrogate PK + UNIQUE (tenant,email) — stability plus business truth.
1-- distributed-safe:2id UUID PRIMARY KEY DEFAULT gen_random_uuid(),3-- vs narrow default:4-- id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY
| key | verdict |
|---|---|
| surrogate INT | compact default |
| UUID | merge-safe, larger |
| natural | UNIQUE, not PK |
Emails change — ids must not. UUIDs let shards merge without coordination; integers keep joins narrow and cache-hot locally. Meaningful values get UNIQUE for validation, never identity.
Three keys side by side
One decision per workload.
1-- default:2-- id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY3-- distributed:4-- id UUID PRIMARY KEY DEFAULT gen_random_uuid()5-- natural guard:6-- email CITEXT UNIQUE NOT NULL
| key | verdict |
|---|---|
| surrogate | compact default |
| UUID | merge-safe |
| natural | UNIQUE not PK |
Surrogate + scoped UNIQUE
Stability plus scoped truth.
1id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,2tenant_id BIGINT NOT NULL,3email CITEXT NOT NULL,4UNIQUE (tenant_id, email)
| constraint | role |
|---|---|
| PK | surrogate id |
| UNIQUE | (tenant,email) scoped |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.