SQL.What is surrogate key and why is it called so ?

What is a Surrogate Key?

Definition

A surrogate key is an artificial, system-generated identifier used as a primary key that has no business meaning.

Typical forms:

  • Auto-increment integer (SERIAL, IDENTITY)
  • Sequence-based BIGINT
  • UUID

Example:

users (
  id BIGSERIAL PRIMARY KEY,
  email TEXT UNIQUE NOT NULL
)

Why is it called surrogate?

Surrogate literally means:

a substitute or replacement

In databases:

  • It replaces a natural/business key
  • It stands in for real-world identity
  • It exists only for technical purposes

You are saying:

“I don’t want my business data to be the identity of this row — I’ll use a surrogate instead.”

That’s the core idea.


Surrogate Key vs Natural Key

AspectSurrogate KeyNatural Key
OriginSystem-generatedBusiness data
MeaningNoneReal-world meaning
StabilityStableCan change
SizeSmall & fixedOften large
PK changesNeverPossible
ORM-friendlyYesOften painful

Why do we use surrogate keys? (important)

1️⃣ Stability

Business data changes:

  • Email can change
  • Passport can be reissued
  • Phone number changes

Changing a primary key is expensive and dangerous.

2️⃣ Performance

  • Small integer keys
  • Smaller indexes
  • Faster joins

Compare:

JOIN ON user_id BIGINT
vs
JOIN ON email TEXT

No contest.

3️⃣ Simpler foreign keys

orders.user_id → users.id

vs

orders.user_email → users.email

Less coupling, less pain.


4️⃣ ORM & microservices friendliness

  • JPA/Hibernate expect stable single-column PKs
  • IDs are easier to pass between services
  • Easier caching and serialization

Typical usage pattern (best practice)

users (
  id BIGSERIAL PRIMARY KEY,
  email TEXT NOT NULL,
  UNIQUE (email)
)

This gives you:

  • Technical identity (surrogate key)
  • Business uniqueness (natural key enforced)

This is the most common real-world design.


Interview traps ❌

  • “Surrogate key is faster” (too shallow)
  • Using surrogate keys without business constraints
  • Thinking surrogate = UUID only
  • Forgetting to enforce natural uniqueness

One-sentence interview answer (perfect)

A surrogate key is a system-generated identifier with no business meaning that substitutes a natural key to provide a stable, efficient primary key.


Bonus follow-up interview question 🚨

“When would you avoid surrogate keys?”

Strong answer:

When the natural key is short, stable, and truly immutable, such as ISO country codes.

This entry was posted in Без рубрики. Bookmark the permalink.