Practice Session: Picking a Good Primary Key
In this session you will choose primary keys for three small tables before reading the suggested answers. The goal is to build a habit: every new table gets a key, chosen on purpose.
Objectives
- Recall what a primary key is: a column, or set of columns, that identifies each row and is never empty.
- Apply three simple tests to candidate keys.
- Choose between a real-world value and a made-up id.
The three tests
A good primary key value should be:
- Unique. No two rows can ever share it.
- Always present. Every row has one from the moment it is created.
- Stable. It never needs to change.
If a candidate fails any test, use a made-up id instead and, if useful, keep the real-world value in a separate column with a UNIQUE rule.
Practice exercise
For each table, write down your chosen key and which tests it passes before reading on.
Table 1: members of a sports club. Columns available: full name, email address, phone number, date joined.
Table 2: countries. Columns available: country name, two-letter country code (such as "JP"), population.
Table 3: library loans. Columns available: member, book, date borrowed, date returned.
Suggested answers
Table 1. Full names fail the uniqueness test: two people can share a name. Email addresses are unique for a while but fail the stability test, because people change them, and some members may not have one. A made-up id is the safe choice:
CREATE TABLE members (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE,
joined DATE NOT NULL
);
Table 2. Two-letter codes come from an international standard (ISO 3166) and are unique and present for every country. They do change occasionally when countries change, which is rare. Many designers happily use the code as the key here; others still prefer an id. Either is defensible if you can explain it.
Table 3. A loan is one member borrowing one book on one date. You could use the three columns together as a composite key, but the same member might borrow the same book twice on one day after returning it. A made-up id avoids that edge case and makes the loan easy to point at from other tables.
Self-check
Why not use the row's position (first row, second row)? Positions change when rows are deleted or sorted. They are not stable.
Can a key be text? Yes. Text keys work; numbers are simply shorter and quicker to compare.
What happens if I insert a duplicate key? The database refuses the insert with an error mentioning a constraint. Try it on your practice table to see the message.
How did you do? If your answers matched or you can defend different ones, you have the habit. If not, re-read the follow-up questions on primary keys and try again with a table of your own.
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.
This page is a free outline. No downloadable files, paid content, courses or certificates are offered on this site.