Skip to content
Part IA Michaelmas Term

Keys and Synthetic Keys

What a key does

A key uniquely identifies each entity instance within an entity set. Given a key value, there is at most one entity with that value. This property is what makes keys the foundation of data integrity: they give us a reliable way to refer to a specific row.

Natural keys

A natural key is a key formed from attributes that have real-world meaning — a National Insurance number, an email address, a vehicle registration plate, an ISBN. They seem appealing because they already exist and users already understand them.

However, natural keys are dangerous for several reasons:

ProblemExample
ReassignmentA dissolved company’s VAT number may be reissued to a new entity after several years.
MutabilityA person changes their email address; an ISBN is reissued for a new edition.
Non-universalityNot every person has a passport number; not every product has a barcode.
Format evolutionTelephone numbering plans change; postcode formats are revised.
Out of your controlYou do not administer NI numbers or domain names. A policy change at the issuing authority can break your schema.
Privacy concernsUsing an email address or NI number as a primary key exposes it in every foreign key reference, in every join, in every log file.

Once a natural key is embedded throughout a schema as foreign keys, changing it requires cascading updates to every referencing table — an operation that may be slow, dangerous under concurrent access, or impossible if the database lacks ON UPDATE CASCADE.

Synthetic keys (surrogate keys)

A synthetic key (surrogate key) is an identifier generated by the database system at insertion time. It has no meaning outside the database and is never exposed to end users.

Common implementations:

MethodStorageProperties
SERIAL / AUTO_INCREMENT4 or 8 bytesSequential, predictable, fast for B-tree insertion
UUID (v4)16 bytesRandom, globally unique, no coordination needed across distributed systems
UUID (v7)16 bytesTime-ordered prefix, good for index locality

The synthetic key approach has clear advantages:

  • Stable: it never changes once assigned.
  • Guaranteed unique: the DBMS enforces uniqueness.
  • Under your control: you define the generation policy, not an external agency.
  • Simple: often just an integer, making foreign keys compact and joins fast.

The only safe approach for large, evolving systems is to use synthetic keys for all primary keys. This is not dogmatic but practical: decades of operational experience show that natural keys eventually cause problems, and the cost of fixing them is far higher than the small upfront cost of adding a surrogate key.

Natural keys still need constraints

Using a synthetic primary key does not mean you abandon natural key constraints. A table of employees might have:

employee_id  SERIAL PRIMARY KEY,    -- synthetic
ni_number    TEXT UNIQUE NOT NULL   -- natural, still enforced

The UNIQUE constraint on ni_number ensures that business logic consistency is maintained — no two employees can share an NI number — but ni_number is not the primary key. If an NI number changes, only one column in one row needs updating, rather than every foreign key reference across the entire database.

Formal definition (relational model preview)

Let R(X)R(X) be a relational schema, where XX is the set of attributes of RR. Let ZXZ \subseteq X be a subset of attributes.

ZZ is a superkey for RR if, for any records uu and vv in any valid instance of RR:

u[Z]=v[Z]    u[X]=v[X]u[Z] = v[Z] \implies u[X] = v[X]

In other words: if two records agree on all attributes in ZZ, they must be the same record (agree on all attributes in XX).

ZZ is a key for RR if:

  1. ZZ is a superkey for RR, and
  2. No proper subset of ZZ is a superkey for RR.

The second condition is minimality: you cannot drop any attribute from the key and still have a superkey. Every relation has at least one key (in the worst case, the set of all attributes XX is a superkey, and if no proper subset is a superkey, then XX itself is a key). One key is typically designated as the primary key for implementation purposes.

Summary

  • A key guarantees that each entity instance can be uniquely identified.
  • Natural keys are appealing but fragile: they are mutable, may not be universal, and lie outside the database’s control.
  • Synthetic keys are stable, guaranteed unique, and under your control — the safe default for any non-trivial system.
  • Natural keys should still carry UNIQUE constraints to enforce business rules.
  • Formally, a superkey is a set of attributes that functionally determines all attributes; a key is a minimal superkey.