Learn SQL
-- constraints.surrogate_uuid_keys

Surrogate vs Natural vs UUID Keys in SQL

Meaningless-and-stable wins PK; meaningful gets UNIQUE

--concept

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.

--how it works
1

Surrogate default

BIGINT IDENTITY — compact, fast, meaningless. Narrow FK fan-out.

2

UUID for merge

gen_random_uuid / v7 — globally unique, larger, random-IO heavier (v7 mitigates).

3

Natural as UNIQUE

Email/SSN meaningful but mutable — UNIQUE + NOT NULL, never PK.

4

Hybrid pragmatic

Surrogate PK + UNIQUE (tenant,email) — stability plus business truth.

--visualization
SURROGATEBIGINT IDENTITY — compact default, narrow joins
Stage 1 / 1
Hybrid: surrogate PK + UNIQUE(tenant,email) — stability plus business truth.
--example
query.sql
1-- distributed-safe:
2id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
3-- vs narrow default:
4-- id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY
Result
keyverdict
surrogate INTcompact default
UUIDmerge-safe, larger
naturalUNIQUE, 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.

--variants

Three keys side by side

One decision per workload.

query.sql
1-- default:
2-- id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY
3-- distributed:
4-- id UUID PRIMARY KEY DEFAULT gen_random_uuid()
5-- natural guard:
6-- email CITEXT UNIQUE NOT NULL
Result
keyverdict
surrogatecompact default
UUIDmerge-safe
naturalUNIQUE not PK
1Local monolith wants narrow ints.
2Multi-region merge wants UUIDs.
3Email changes — identity must not.

Surrogate + scoped UNIQUE

Stability plus scoped truth.

query.sql
1id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
2tenant_id BIGINT NOT NULL,
3email CITEXT NOT NULL,
4UNIQUE (tenant_id, email)
Result
constraintrole
PKsurrogate id
UNIQUE(tenant,email) scoped
1Joins ride narrow id.
2Business rule enforced per tenant.
3Composite B-tree serves scoped lookups.
--key facts
Surrogatecompact, stable default
UUIDdistributed merge-safety, larger
NaturalUNIQUE constraint, not PK
--use cases
Key review: PK stability vs business-rule UNIQUEs
Shard/merge planning: UUIDv7 rollout
--under the hood
Random UUIDs fragment B-trees — v7 time-ordered prefixes append mostly-sorted; fillfactor tuning helps.
CITEXT avoids LOWER() indexes for emails where available.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.