Choosing Between Natural and Surrogate Keys
Every table needs a primary key. You can use a value that already exists in the real world, a natural key, or one the database makes up, a surrogate key. This page compares the two with simple examples and gives a verdict for common beginner tables.
Natural keys
A natural key is real information that already identifies something.
Examples: a country code such as FR, a product's printed code, an email address, a username.
CREATE TABLE countries (code TEXT PRIMARY KEY, name TEXT NOT NULL);
Pros:
- Meaningful: you can read
FRand know what it is. - No extra column needed.
- Prevents duplicates of the real-world thing automatically.
Cons:
- Real-world values can change: people change emails and usernames.
- They can be missing at first: a new customer might not have an email yet.
- They can be longer than a number, and they get copied into every table that refers to them.
- Someone else may control them, and can change the rules.
Surrogate keys
A surrogate key is a value with no meaning outside the database, usually a number the database generates.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
username TEXT NOT NULL UNIQUE
);
Pros:
- Never needs to change, whatever happens to the real-world data.
- Always present from the moment the row is created.
- Small and simple to use in foreign keys.
Cons:
- Means nothing on its own;
17tells you nothing about the row. - You must add UNIQUE rules separately if the natural value should not repeat, as with
emailabove.
Side by side
| Question | Natural key | Surrogate key |
|---|---|---|
| Can it change? | Sometimes | No |
| Always available at insert? | Not always | Yes |
| Meaningful to humans? | Yes | No |
| Stops duplicate real-world items? | Yes | Only with an extra UNIQUE rule |
| Size in other tables | Varies | Small |
Verdict for common tables
| Table | Suggested key | Why |
|---|---|---|
| users, customers, members | Surrogate id plus UNIQUE on email or username | Emails and usernames change |
| countries, currencies | Natural standard code can work | Codes are short and stable |
| products | Surrogate id plus UNIQUE on product code | Codes may be reissued or changed |
| orders, posts, events | Surrogate id | No natural identifier exists |
| linking tables such as enrolments | The pair of foreign keys | The pair is the natural identity |
The popular combination
The most common beginner-friendly pattern is: a surrogate id as the primary key, plus a UNIQUE rule on the natural value. You get stability from the id and duplicate protection from the rule.
A note on privacy
Never use sensitive values, such as national identity numbers, as keys. Keys get copied into many tables, logs and URLs.
Practise the decision
Try the eight primary key scenarios, and record your choice with the primary key decision template.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.