A3.2.4 · Database design
Building tables
By the end you can
Construct tables with primary keys, foreign keys and constraints, and see how the database defends them.
Building a table is where the design becomes real. Each column gets a data type, and the table gets its keys and : rules the database itself enforces, so bad data is refused instead of quietly stored. In SQL this is the CREATE TABLE statement.
CREATE TABLE#
Here are the tables of a school clubs database. The “one” side comes first, because a foreign key can only point at a table that already exists.
CREATE TABLE Teacher (TeacherID INT PRIMARY KEY, Name VARCHAR(30) NOT NULL);
CREATE TABLE Club (ClubID INT PRIMARY KEY, Title VARCHAR(30) NOT NULL, TeacherID INT NOT NULL, FOREIGN KEY (TeacherID) REFERENCES Teacher(TeacherID));
CREATE TABLE Member (ClubID INT NOT NULL, StudentID INT NOT NULL, Joined DATE NOT NULL, PRIMARY KEY (ClubID, StudentID), FOREIGN KEY (ClubID) REFERENCES Club(ClubID) ON DELETE CASCADE);
PRIMARY KEYmakes the column unique and never empty. For a composite key, list the columns:PRIMARY KEY (ClubID, StudentID).FOREIGN KEY (col) REFERENCES Table(col)says every value must match a primary key in that table. This is the line that creates the relationship.NOT NULLforbids an empty value;UNIQUEforbids repeats in a column that is not the primary key.ON DELETE CASCADEsays that deleting a parent row also deletes the rows that point at it.
Try to break it#
The sandbox below has these three tables with a little data in them. Run the examples, and see how the database defends itself: duplicate keys, a club for a teacher who does not exist, a wrong type, and deleting a teacher who still has clubs. Then try the cascade: delete the Robotics club and look at the Member table.
Teacher
| TeacherIDPK | Name |
|---|---|
| 1 | Bobby Williams |
| 2 | Dima White |
2 rows. TeacherID INT, Name TEXT(30).
Adding a second student with StudentID 105 to the Student table.
Adding an order for CustomerID 999 when no such customer exists.
Saving a new member without a name.
Putting the word “soon” in a DATE column.
Deleting a club also removes its memberships automatically.
0 of 5 matched
Check yourself
Keys and constraints
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
4 questions · 13 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Construct the SQL statement to create a table Crime with the attributes CrimeID (primary key, integer), Location (text up to 40 characters), Type (text up to 20 characters) and WitnessID (integer, a foreign key to a Witness table with primary key WitnessID).
[4]Construct: Draw or write it out (a table, a diagram, a program).
2.Explain what happens when you try to delete a customer who still has orders, and how ON DELETE CASCADE changes this.
[3]Explain: Give reasons: say what happens AND why, or how one thing causes another.
3.A table Membership has the columns StudentID, ClubID and Joined. Explain why its primary key should be StudentID and ClubID together.
[3]Explain: Give reasons: say what happens AND why, or how one thing causes another.
4.Describe three constraints a database can enforce on a table.
[3]Describe: A detailed account: say what happens, step by step if it is a process.
Past papers
Old exam questions on this topic
From HL Paper 1, May 2014 to November 2025. These papers were set on the previous syllabus, so the wording may differ from today's exam, but the topic is the same. Find the questions and markschemes in your school's past-paper pack.
| Paper | Question | Marks | About |
|---|---|---|---|
| May 2025 TZ2 | 2(f) | 2 | Paper 2 Option A: keys and constraints (same in TZ3) |
| November 2025 | 3(a) | 2 | Paper 2 Option A: keys and constraints |
| May 2024 | 1(f) | 3 | Paper 2 Option A: keys and constraints |
| November 2024 | 1(b) | 3 | Paper 2 Option A: keys and constraints |
| November 2023 | 1(a) | 4 | Paper 2 Option A: keys and constraints |
| November 2023 | 1(c) | 3 | Paper 2 Option A: keys and constraints |
| November 2019 | 1(f) | 4 | Paper 2 Option A: keys and constraints |
| November 2019 | 2(f) | 3 | Paper 2 Option A: keys and constraints |
In a nutshell
- CREATE TABLE lists each column with its data type, then the constraints: NOT NULL, PRIMARY KEY, UNIQUE and FOREIGN KEY … REFERENCES.
- A table with a composite key lists PRIMARY KEY (a, b). Create the “one” side first, because a foreign key can only point at a table that exists.
- The database enforces the keys: it refuses a duplicate primary key, a foreign key that points at nothing, and an empty NOT NULL column.
- Deleting a row that other rows point at is refused, unless the foreign key was defined with ON DELETE CASCADE, which deletes the dependent rows too.
- Keys and constraints are how a database protects its own integrity, so planning them is part of building the table.
