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

  1. A members table for a sports club. Options: the member's full name, their email, or a generated id.
  2. A countries table listing the world's countries. Options: a generated id, or the two-letter country code.
  3. A book_loans table recording which member borrowed which book on which day. Options: a generated id, or the pair (member_id, book_id).
  4. An enrolments table 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.
  5. A cars table for a garage. Options: the number plate, or a generated id.
  6. A sensor_readings table with millions of rows added constantly. Options: a generated id, or the reading time.
  7. An employees table. Options: a national identity number, or a generated id.
  8. A settings table with exactly one row per user. Options: user_id as the key, or a separate generated id.

Explanations

  1. Generated id. Names repeat and change. Emails change too, and people share family emails. An id never needs to change.
  2. 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.
  3. Generated id. The same member can borrow the same book more than once on different days, so (member_id, book_id) is not unique.
  4. 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.
  5. 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.
  6. Generated id. Two readings can arrive at the same moment, so time alone is not unique.
  7. 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.
  8. 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

PointsWhat it suggests
0 to 3Re-read Practice Session: Picking a Good Primary Key
4 to 6Good instincts; look at Choosing Between Natural and Surrogate Keys
7 to 8Ready to design small schemas with confidence

The questions behind every choice

When you are unsure, ask these four questions about a candidate key:

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.