Assess Your Primary Key Choices With Eight Scenarios
A primary key is the column, or set of columns, that uniquely identifies each row. Choosing well saves trouble later. Read each scenario, decide which key you would use, then compare with the explanation. Give yourself a point for each sound choice.
Scenarios
- A
memberstable for a sports club. Options: the member's full name, their email, or a generated id. - A
countriestable listing the world's countries. Options: a generated id, or the two-letter country code. - A
book_loanstable recording which member borrowed which book on which day. Options: a generated id, or the pair (member_id, book_id). - An
enrolmentstable linking students to courses, where a student can enrol in a course only once. Options: a generated id plus a UNIQUE rule on (student_id, course_id), or (student_id, course_id) as the key. - A
carstable for a garage. Options: the number plate, or a generated id. - A
sensor_readingstable with millions of rows added constantly. Options: a generated id, or the reading time. - An
employeestable. Options: a national identity number, or a generated id. - A
settingstable with exactly one row per user. Options: user_id as the key, or a separate generated id.
Explanations
- Generated id. Names repeat and change. Emails change too, and people share family emails. An id never needs to change.
- Either is reasonable. Country codes from the ISO 3166 standard are short, stable and meaningful. A generated id also works. This is a classic case where a "natural" key is acceptable.
- Generated id. The same member can borrow the same book more than once on different days, so (member_id, book_id) is not unique.
- Either works. Using the pair as the key, or an id plus a UNIQUE rule on the pair, both stop duplicate enrolments. The important part is that the pair must be unique.
- Generated id. Number plates can change, and a car may be transferred or re-registered. Keep the plate as a normal column, perhaps with a UNIQUE rule.
- Generated id. Two readings can arrive at the same moment, so time alone is not unique.
- Generated id. Identity numbers are sensitive personal data. Using them as a key spreads them into every table that links to employees. Store them carefully, if at all, and never as the key.
- user_id as the key is tidy, because it naturally allows only one settings row per user. A separate id with a UNIQUE rule on user_id also works.
Scoring
| Points | What it suggests |
|---|---|
| 0 to 3 | Re-read Practice Session: Picking a Good Primary Key |
| 4 to 6 | Good instincts; look at Choosing Between Natural and Surrogate Keys |
| 7 to 8 | Ready to design small schemas with confidence |
The questions behind every choice
When you are unsure, ask these four questions about a candidate key:
- Is it always unique? Even in unusual cases?
- Is it always present? A key can never be empty.
- Will it ever change? Changing keys is painful.
- Is it sensitive? Keys get copied into other tables, logs and URLs.
If any answer worries you, a generated id is usually the safer choice, with the meaningful value kept as an ordinary column.
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.