Course Outline: Understanding Primary Keys From Scratch

Primary keys sound like a small detail, but nearly every database mistake beginners make traces back to them. This free outline breaks the topic into five short modules. Work through one module per sitting with a practice database open.

What you will learn

By the end you should be able to explain what a primary key is, choose a sensible one for a new table, and use it to link tables together. No prior knowledge beyond a basic SELECT is assumed.

Module 1: what a primary key is

Goal: say in one sentence what a primary key does.

A primary key is a column (or a group of columns) whose value is different for every row and is never empty. It is how the database, and you, point at exactly one row. Think of a student number: two students can share a name, but never a number.

Task: look at a table you made earlier. Which column, if any, already identifies each row uniquely?

Module 2: letting the database number rows

Goal: create a table with an automatic id.

-- SQLite
CREATE TABLE pets (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
-- PostgreSQL
CREATE TABLE pets (id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, name TEXT NOT NULL);
-- MySQL
CREATE TABLE pets (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL);

Different spelling, same idea: the database picks the next number for you.

Task: insert three pets without giving an id, then SELECT them and see the numbers.

Module 3: natural or made-up keys

Goal: know when a real-world value can be the key.

Real values such as email addresses or product codes are tempting, but they can change or collide. A made-up id plus a UNIQUE rule on the real value gives you the best of both.

Task: decide keys for a countries table and a customers table, and write down why.

Module 4: keys made of two columns

Goal: understand composite keys.

A table recording which pet visited which vet on which day might use more than one column together as its key. Composite keys are common in "linking" tables.

Task: design a table that records which students are enrolled in which classes.

Module 5: foreign keys

Goal: link two tables safely.

CREATE TABLE visits (
  id     INTEGER PRIMARY KEY,
  pet_id INTEGER NOT NULL REFERENCES pets(id),
  note   TEXT
);

REFERENCES pets(id) makes pet_id a foreign key: the database can refuse visits for pets that do not exist. (In SQLite, run PRAGMA foreign_keys = ON; first, because the check is off by default.)

Task: try to insert a visit for pet 999 and read the error.

Readings

ModuleReading on this site
1 to 3Primary keys: the questions beginners ask next
3Practice session: picking a good primary key
4 and 5A picture guide to keys linking two tables
5How to add a foreign key between two tables

You are done when

You can look at any new table idea and say, without hesitation, what its primary key will be and why.

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.