Types of Database Keys


Types of Database Keys

Anna
Alright Michael, let's talk about database keys. There are a ton of different types: primary, candidate, super, alternate, foreign, surrogate, natural, intelligent, simple, composite, compound… it can get overwhelming fast.

Michael
It really can. But the key idea is that a key uniquely identifies a row in a table. And to make it concrete, let's start with a classic example: a US states table.

Anna
Right. Each state has a row, and we need a column that can tell states apart. State code, like 'MA' for Massachusetts, is a perfect fit. It's stable and unique.

Michael
Stable meaning it's not going to change. Unlike, say, a mobile phone number for a person, which people swap all the time. So state code is a great candidate for the primary key.

Anna
Exactly. When we designate state code as the primary key, the database enforces uniqueness. No two rows can have the same state code, and it can't be null. That gives us a reliable shorthand to refer to a state.

Michael
But state name is also unique, right? Every US state has a distinct name. So that could work as a primary key too.

Anna
Yes! Both state code and state name are what we call candidate keys. They're the candidates we consider for the primary key. State name is a bit longer, but in principle it's valid.

Michael
What about state capital? Could that be a candidate key? I mean, no two state capitals share a name right now.

Anna
That's a trick. State capitals have changed in the past. For example, Georgia's capital was once Augusta, and if it changed back, you'd have two Augustas — Georgia's and Maine's. So uniqueness across time isn't guaranteed. State capital is not a candidate key.

Michael
Good point. A key has to be able to uniquely identify not just current rows, but any possible future row too. Otherwise it's not reliable.

Anna
Now suppose a designer gets it wrong and makes a primary key of (state_code, region). That would be a super key — it contains all the attributes needed for uniqueness plus extra ones.

Michael
And because it has an extra attribute, it's not irreducible. The term for a minimal unique identifier is a candidate key. A super key is any superset of a candidate key.

Anna
Right. So after choosing state code as the primary key, we still have another candidate key: state name. We can enforce its uniqueness by setting it as an alternate key. That tells the database to maintain a uniqueness constraint on it.

Michael
And then there are foreign keys. Imagine a voters table that references a state. We don't want a voter's state set to 'QX' if that's not a valid state code. So we set up a foreign key constraint from the voters table's state column to the state code in the US states table.

Anna
That ensures every value in the voters table matches an existing state code. It guarantees referential integrity, so joins between the tables work reliably.

Michael
Now, in some designs, we might replace a natural key like state code with an arbitrary numeric ID, say a surrogate key. For example, a state_id of 25 stands for Georgia, but only within the database. No one in the real world knows that 25 means Georgia.

Anna
That's the essence of a surrogate key: its meaning is only inside the database. The original state code is a natural key because people use it in real life, like on envelopes.

Michael
But surrogate keys have a pitfall. If you don't also enforce uniqueness on the natural key, you could end up with two rows for the same state, just with different surrogate IDs. That would be wrong.

Anna
Exactly. A surrogate key isn't just a row ID; it must represent one specific real-world thing. You need an alternate key or a careful insertion process to avoid duplicates.

Michael
So when do you use a natural key versus a surrogate? If a stable natural identifier already exists, like a postal code, using it as the primary key is fine. But for something like a recipe, there's no obvious natural key, so a surrogate is sensible.

Anna
And sometimes a natural key exists but has limitations. For a books table, ISBN might seem perfect, but many ebooks don't have ISBNs. So you'd create your own book number, making it a surrogate — unless you publicize it, then it becomes a natural key.

Michael
Interesting. So the line between natural and surrogate can blur. Now, keys can also have multiple columns. A simple key has one attribute; a composite key has more than one. For a coffee options table, the combination of coffee type and cup size uniquely identifies a price row.

Anna
And if that composite key is the primary key, any foreign key referencing it must also be composite. Some people use the term compound key specifically when the composite key includes at least one foreign key, like in a junction table linking customers and accounts.

Michael
Right. In that customer_account table, the primary key is (customer_id, account_id), and both columns are foreign keys, so it's a compound key.

Anna
Finally, there's the intelligent key. That's a natural key made of multiple parts, each with its own meaning. For example, a product code 'CLS-TRN-016' where 'CLS' means classic and 'TRN' means train set.

Michael
But that can be risky. People might rely on extracting the subcategory from the code, which breaks if the coding scheme changes. Better to store subcategory in a dedicated column.

Anna
Exactly. Intelligent keys can also cause trouble when recategorization happens. If the company stops using classic/modern and switches to battery operated, the code format conflicts. So care is needed. The best intelligent keys use components that are truly essential and stable, like a customer date of birth plus a sequence number.

Michael
So we've covered a lot: primary, candidate, super, alternate, foreign, surrogate, natural, simple, composite, compound, and intelligent keys. Each plays a role in database design.

Anna
And understanding them helps you design tables that are reliable, avoid anomalies, and keep data consistent. Hopefully this clears up the jargon for everyone.

Ask anything
We've read this whole study set, so questions about it get the best answers.