Natural Primary Key

Difficulty: Intermediate

Question

What is a natural key, and how does it compare with a surrogate key for a primary key?

Answer

A natural key is a primary key made from data that already exists in the business domain and uniquely identifies the entity, such as an ISBN for a book, an IFSC code for a bank branch, or a GSTIN for a registered business. A surrogate key is an artificial identifier with no business meaning, such as an auto-increment integer or a UUID. Most production systems use a surrogate primary key and enforce the natural key with a separate UNIQUE constraint.

Candidate, natural and surrogate keys

  • Candidate key: any minimal set of columns that uniquely identifies a row. A table can have several.
  • Natural key: a candidate key taken from real-world data.
  • Surrogate key: a system-generated value added only to identify rows.

Advantages of natural keys

  • No extra column; the identifier already means something.
  • Uniqueness is enforced by the primary key itself, so duplicates of the real entity cannot sneak in.
  • Joins can sometimes be skipped because the child table already holds a meaningful value (for example country_code = 'IN').
  • Good fits: ISO country and currency codes, IFSC codes, airport codes, ISBN in a library system.

Why natural keys often go wrong

  • They change. Emails, phone numbers, usernames and even company names change. Updating a primary key means updating every foreign key that references it.
  • They are not as unique as assumed. Names collide, some books lack an ISBN, phone numbers get recycled by telecom operators.
  • They may be missing at creation time, such as a student who does not have a university roll number yet.
  • Privacy and compliance. Using Aadhaar or PAN as a key spreads sensitive identifiers into every child table, logs and URLs. Under India's Digital Personal Data Protection Act 2023 and Aadhaar rules, that is a liability.
  • Size. A long text key makes every index and foreign key bigger than an 8-byte integer.

Comparison

AspectNatural keySurrogate key
MeaningBusiness meaningNone
StabilityCan changeNever changes
SizeOften wide text8-byte BIGINT or 16-byte UUID
Duplicate protectionBuilt inNeeds a separate UNIQUE constraint
Exposure riskMay leak sensitive dataSafe to expose (UUIDs especially)
Good examplesCurrency code, IFSCUsers, orders, payments

The practical recommendation

Use a surrogate primary key (BIGINT GENERATED ALWAYS AS IDENTITY, or a time-ordered UUIDv7 if IDs are generated across services) and add a UNIQUE constraint on the natural key. You get stable joins plus protection against duplicate real-world entities. Keep natural primary keys for small, stable reference tables where the code itself is the identity.

Common mistakes

  • Using a surrogate key and forgetting the UNIQUE constraint on the natural key, which lets the same GSTIN be registered twice.
  • Choosing email as a primary key, then supporting "change my email".
  • Exposing sequential integer IDs in public URLs, which lets anyone enumerate records.
Natural key vs surrogate key. The common answer is both: surrogate primary key plus a unique natural key.
Natural keySurrogate key
ExampleIFSC code, ISBN, GSTINBIGINT identity or UUIDv7
Can it change?Yes, sometimesNo
Prevents duplicate entitiesYes, by itselfOnly with a UNIQUE constraint
Best forSmall stable reference tablesCore entities like users and orders

Code examples

Surrogate primary key plus unique natural key

CREATE TABLE merchants (
  id         BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, -- surrogate
  gstin      CHAR(15) NOT NULL UNIQUE,                        -- natural key, enforced
  legal_name TEXT     NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- Natural primary key is fine for a stable reference table
CREATE TABLE bank_branches (
  ifsc      CHAR(11) PRIMARY KEY,
  bank_name TEXT NOT NULL,
  city      TEXT NOT NULL
);

Merchants get a stable internal id while GSTIN uniqueness is still guaranteed; branches use the IFSC code directly because it is the identity.

Key points

  • Natural key: real-world identifier like ISBN, IFSC or GSTIN. Surrogate key: generated with no meaning.
  • Natural keys can change, collide or be missing, which breaks foreign keys.
  • Avoid sensitive identifiers like Aadhaar or PAN as keys.
  • Best practice: surrogate primary key plus UNIQUE constraint on the natural key.
  • Natural primary keys suit small, stable reference tables.

Common follow-up questions

  • What is a candidate key and how is it different from a primary key?
  • Auto-increment integers or UUIDs: which would you choose and why?
  • What is UUIDv7 and why is it better for B-tree indexes than UUIDv4?
  • How would you change a natural primary key that is referenced by ten tables?

Interview tips

  • Define natural, surrogate and candidate keys crisply before comparing them.
  • Recommend the hybrid approach and explain why; interviewers want a decision, not a list.
  • Give an example of a natural key that looked stable but changed, like a phone number or email.

Concepts covered

Natural Key, Surrogate Key, Primary Key, Unique Constraint, Candidate Key

More Database Design questions