Surrogate vs. Natural Keys

Difficulty: Beginner

Question

What is a surrogate key, how is it different from a natural key, and which would you choose as a primary key?

Answer

A surrogate key is a primary key generated by the system, such as an auto-increment integer or a UUID, that has no meaning outside the database. A natural key is a column, or set of columns, that already identifies the entity in the real world, like a PAN number, an email address or an ISBN. Most modern schemas use a surrogate primary key and still enforce the natural key with a UNIQUE constraint, getting stable joins and real-world uniqueness at the same time.

Examples of each

EntityNatural key candidateSurrogate key
CustomerEmail, phone number, PANcustomer_id BIGINT
BookISBNbook_id
VehicleRegistration numbervehicle_id
CountryISO code INcountry_id

Why surrogate keys are the common default

  • Stability. Natural values change. Users change emails and phone numbers, companies rebrand, vehicle registrations are transferred. If that value is the primary key, every foreign key referencing it must change too.
  • Compactness. An 8-byte BIGINT is smaller and faster to compare and index than a 50-character email or a composite of three columns. This matters because the primary key is copied into every foreign key and, in InnoDB, into every secondary index.
  • Privacy. Using a PAN or Aadhaar number as a key spreads sensitive data into URLs, logs and every child table. Aadhaar in particular has legal restrictions on storage and display.
  • No surprises. "Unique" business values sometimes are not. Two people can share a phone number; data entry mistakes happen; formats change.
  • ORM friendliness. Frameworks like Hibernate, Django and Rails assume a single-column id.

When natural keys are fine

  • The value is truly immutable and universally unique, such as an ISO country code or currency code (INR).
  • Lookup or reference tables where the code is what users and other systems use.
  • Junction tables, where the composite of two foreign keys (student_id, course_id) is a perfectly good primary key.

How to generate surrogate keys

  • Auto-increment / identity: BIGINT GENERATED ALWAYS AS IDENTITY in PostgreSQL (preferred over the older SERIAL), AUTO_INCREMENT in MySQL. Simple and compact but guessable, and hard to generate across many databases.
  • UUID: globally unique without coordination, good for distributed systems and public ids. Prefer time-ordered UUIDv7 over random UUIDv4 for index performance.
  • Snowflake-style ids: 64-bit time-ordered ids generated by services, used by companies such as Twitter and Discord.

The common mistake

Adding a surrogate key and forgetting the unique constraint on the natural key. Then the database happily stores two customers with the same email under different ids, and you get duplicate accounts. The surrogate key identifies the row; the unique constraint protects the business rule.

Code examples

Surrogate primary key plus natural key uniqueness

CREATE TABLE customers (
  id          BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,  -- surrogate
  email       TEXT NOT NULL,
  phone       TEXT,
  pan_number  CHAR(10),
  created_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
  CONSTRAINT uq_customers_email UNIQUE (email),     -- natural key still enforced
  CONSTRAINT uq_customers_pan   UNIQUE (pan_number)
);

-- Child tables reference the compact, stable id
CREATE TABLE orders (
  id          BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  customer_id BIGINT NOT NULL REFERENCES customers(id)
);

If a customer changes their email, only one row changes. Orders keep pointing at the same id.

Key points

  • Surrogate keys are system-generated with no business meaning; natural keys come from the real world
  • Natural values often change or turn out not to be unique, which makes them risky primary keys
  • Keep the natural key as a UNIQUE constraint even when using a surrogate primary key
  • Natural keys suit immutable codes (ISO country, currency) and junction tables
  • Prefer IDENTITY over SERIAL in PostgreSQL and UUIDv7 over UUIDv4 when using UUIDs

Common follow-up questions

  • What are the trade-offs of UUIDs vs auto-increment integers as surrogate keys?
  • Should a junction table have its own surrogate id?
  • What is a candidate key and an alternate key?
  • Why is exposing auto-increment ids in public URLs a risk?

Interview tips

  • Give a concrete example of a natural key that changed, like a user updating their email
  • Always pair 'use a surrogate key' with 'and keep a unique constraint on the natural key'
  • Show nuance by naming cases where natural keys are fine, such as country codes

Concepts covered

Surrogate Key, Natural Key, Primary Key, Unique Constraint, Identity Column

More Database Design questions