Quick revision
The short answer
A natural key comes from the domain, such as an ISO country code, an email or a VAT number, while a surrogate key is generated with no business meaning, such as an identity column, a sequence value or a UUID. Teams prefer surrogate primary keys because natural keys can change or be re-issued, and changing a primary key ripples into every foreign key, cache and URL, while a surrogate key is small, stable and uniform, which suits JPA and joins. The natural key must still carry a UNIQUE constraint, otherwise the surrogate key quietly allows duplicates.
What to remember
In this answer, we defined natural keys as values from the domain and surrogate keys as generated values with no meaning, saw that changing a natural primary key ripples through foreign keys, caches, URLs and other services, and that surrogate keys are compact, stable and uniform, which suits JPA and joins. We also saw that the natural key must keep a UNIQUE constraint or duplicates slip in, and that small immutable codes such as currency codes are fine as primary keys of reference tables.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A natural key comes from the domain, such as an ISO country code, an email or a VAT number, while a surrogate key is generated with no business meaning, such as an identity column, a sequence value or a UUID. Teams prefer surrogate primary keys because natural keys can change or be re-issued, and changing a primary key ripples into every foreign key, cache and URL, while a surrogate key is small, stable and uniform, which suits JPA and joins. The natural key must still carry a UNIQUE constraint, otherwise the surrogate key quietly allows duplicates.
Why this question is asked
Choosing the primary key is one of the first decisions for every table and one of the hardest to undo, because every foreign key, index, cache entry and API URL repeats it. Interviewers ask this to see whether we know the trade-off rather than a rule, and especially whether we remember the part people forget: the natural key still needs protecting. In this answer, we will define both kinds of key with examples, see what goes wrong when a natural key changes, look at why surrogate keys suit ORMs and joins, show the duplicate problem that a missing unique constraint causes, and finish with the cases where a natural primary key is the better choice.
1Natural Keys and Surrogate Keys
A natural key is a value that already identifies something in the real world, before any database exists: the ISO country code DE, a currency code EUR, a VAT number, an ISBN, an employee's email. A surrogate key is a value we generate only to identify the row, with no meaning outside the database: 1, 2, 3 from an identity column or a sequence, or a UUID such as 3f2c9a1e-....
Both can be primary keys. The question is which one other tables should reference.
In PostgreSQL 10 and later, and in standard SQL, GENERATED ALWAYS AS IDENTITY is the modern way to get a surrogate key; the older serial type does the same with a sequence behind it. MySQL uses AUTO_INCREMENT.
2What Happens When a Natural Key Changes
Natural keys feel permanent until they are not. People change their email address. Companies re-register and get a new VAT number. Product codes get re-issued for a different product. Even country codes change, as when a country is renamed or splits.
If email were the primary key of users, every table referencing users would store the email. Changing Ana's email then means updating her row and every order, session, audit entry and ticket that references her, inside one transaction, with ON UPDATE CASCADE or by hand. Outside the database it gets worse: caches keyed by email, URLs like /users/ana@example.com, events in Kafka and other services' databases all hold the old value and nothing cascades there.
| Where the key is repeated | Email as primary key | Surrogate id as primary key |
|---|---|---|
| users row | update the key | update one column |
| foreign keys in orders, sessions, audit | cascade to every row | unchanged |
| caches, URLs, search indexes | stale, must be found and fixed | unchanged |
| events and other services | old value lives on | unchanged |
3Why Surrogate Keys Suit ORMs and Joins
Surrogate keys are compact, a bigint is 8 bytes compared with an email of 20 to 60, so foreign key columns and their indexes are smaller and joins are cheaper. They are uniform, every table has an id column of the same type, which makes code generic and conventions simple. And they are stable by construction, since nothing in the business can ever give us a reason to change them.
ORMs like JPA and Hibernate are built around that model. An entity has one @Id, ideally generated, and never changes it, because the persistence context uses the id to track entity identity. Composite or mutable natural keys are possible with @EmbeddedId or @IdClass, but they complicate mapping, equals and hashCode, and relationships.
4The Natural Key Must Still Be Unique
Here is the trap. Once the table has a surrogate primary key, the database will happily accept two rows with the same email, because each row gets a new id and the primary key is satisfied.
So the surrogate key does not replace the natural key, it hides it. A double-click on a sign-up form or a retried import creates duplicates, and later someone has to merge them, which is far harder than preventing them. The rule is: surrogate key for references, UNIQUE constraint on the natural key for correctness. Sometimes the uniqueness needs care, such as case-insensitive email with a unique index on lower(email), or uniqueness only among active rows with a partial index.
5When a Natural Primary Key Is the Better Choice
For small reference tables whose keys are short, standardised and effectively immutable, a natural primary key is often better: currency codes such as EUR, ISO country codes, language tags. Rows that reference them become readable without a join, currency = 'EUR' instead of currency_id = 3, and the code is the identifier everyone uses anyway.
Join tables are another case. book_author(book_id, author_id) has a natural composite key made of two surrogate keys, and adding a third id column brings nothing. The decision is about stability and size, not ideology: if the value can change or be re-issued, reference it through a surrogate key; if it is truly fixed and small, it can be the key.
6Follow-up questions
Would you use email as the primary key of users?
No. Emails change, people lose access to old addresses, and some systems treat addresses case-insensitively while others do not, so the value is neither stable nor simple to compare. As a primary key it would spread into every foreign key, URL and event, and also expose personal data in places such as logs and links. I would use a surrogate id, keep email NOT NULL with a unique index on lower(email), and let users change it with a verification flow.
Should the surrogate key be a sequence or a UUID?
A bigint identity is smallest and fastest, and is fine when one database generates ids. UUIDs suit ids created in many services or clients, and avoid exposing counts in URLs. Random UUIDv4 values scatter inserts across a B-tree index; time-ordered UUIDv7, standardised in RFC 9562, keep inserts mostly sequential, so check your database and library support for generating them.
Is a surrogate key enough to make a row unique?
Only technically. It makes each row distinguishable, but it says nothing about whether two rows describe the same real thing. That is the natural key's job, which is why it keeps a UNIQUE constraint.
7Conclusion
In this answer, we defined natural keys as values from the domain and surrogate keys as generated values with no meaning, saw that changing a natural primary key ripples through foreign keys, caches, URLs and other services, and that surrogate keys are compact, stable and uniform, which suits JPA and joins. We also saw that the natural key must keep a UNIQUE constraint or duplicates slip in, and that small immutable codes such as currency codes are fine as primary keys of reference tables.
Open with
A natural key is a value that identifies something in the real world, like an ISO country code, a VAT number or an email. A surrogate key is a value we generate only to identify the row, such as an identity column, a sequence or a UUID, with no business meaning.
Then, if they want more
Many teams use a surrogate primary key even when a natural key exists because natural keys change more often than we expect: people change emails, VAT numbers are re-issued, product codes get reused.
A primary key is copied into every foreign key, and also into caches, URLs, events and other services, so changing it means cascading updates in the database and stale values everywhere outside it.
A surrogate key never has a business reason to change, it is small, like an eight-byte bigint, so foreign keys and joins are cheap, and it is uniform across tables, which is exactly what JPA expects with a generated @Id. The important part is that the natural key still needs a UNIQUE constraint.
Otherwise each duplicate insert just gets a new id and we end up with two customers with the same VAT number, which is much harder to merge later than to prevent. And natural keys are fine as primary keys when they are small and truly immutable, such as currency or country codes in reference tables.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 10
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain natural keys and surrogate keys with an example of each. Why do many teams use a...": https://scrivio-interview-library.netlify.app/databases/data-modeling/3a9ac35a7c/