Primary Keys: The Questions Beginners Ask Next
You know that a primary key is the column that uniquely identifies each row. Once you start designing real tables, the next questions arrive quickly. Here they are, answered one at a time.
Choosing a key
What is the difference between a natural key and a surrogate key?
A natural key is something that already exists in the real world, like an ISBN for a book or a country code such as "FR". A surrogate key is a number or code the database makes up just to identify the row, usually id. Natural keys feel meaningful, but real-world values can change or turn out not to be unique after all. Most beginners are safest with a surrogate id plus a separate unique rule on the natural value.
CREATE TABLE books (
id INTEGER PRIMARY KEY,
isbn TEXT NOT NULL UNIQUE,
title TEXT NOT NULL
);
Can a primary key be more than one column?
Yes. That is called a composite key. A table that records which student is in which class might use (student_id, class_id) together, because the pair is unique even though each column on its own repeats.
Should I use a number or a UUID?
A UUID is a long random-looking identifier such as 3f2b.... It can be created anywhere without asking the database, which helps when several systems create records. Plain whole numbers are shorter, easier to read and simpler to start with. For a first app, numbers are fine.
How keys behave
Why are there gaps in my auto-increment ids?
The database hands out numbers in order, but it does not go back to fill holes. If you delete row 5, or an insert fails halfway, number 5 is simply skipped. Gaps are normal and harmless. Never use ids to count rows; use COUNT(*) instead.
Can a primary key value change?
Technically you can update it, but you usually should not. Other tables point to that value, so changing it means changing every reference too. Pick keys that never need to change, which is another reason surrogate ids are popular.
Can a primary key be empty?
No. A primary key column cannot be NULL (the "unknown" value), and every value must be different. The database enforces both rules for you and rejects rows that break them.
Keys and other tables
What is a foreign key, then?
A foreign key is a column in one table that stores the primary key of a row in another table. An orders table might have a customer_id column pointing to customers.id. The database can check that every customer_id really exists. Our picture guide to keys linking two tables shows this visually.
Does a primary key make searches faster?
Yes. Databases build an index (a sorted lookup structure, a bit like the index at the back of a book) for the primary key automatically, so finding one row by its key is quick even in a huge table.
What should I practise next?
Work through the practice session on picking a good primary key. It gives you three tables and asks you to choose keys before showing sample answers.
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.