Table relationships
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Why several tables?
Imagine the league kept everything in one big table: each Codémon with its trainer's name, hometown and badges.
Alex's hometown and badges are written twice, Morgan's too. If Morgan wins a badge, you must fix every row — forget one, and the data contradicts itself.
The fix is to split the data into two tables: one for the trainers, one for the Codémons.
Each trainer is now stored once. Each codemon row only keeps the trainer_id of its owner — a small number instead of a copy of the whole trainer.
Why bother?
No repetition means less storage, a single place to update, and no contradictions. Splitting data like this is called normalization.
Exercise 2 — The problem with one big table
What is the main problem if every Codémon row also contains its trainer's hometown and badges?
Hint : Think about what happens when Morgan wins a new badge.
Exercise 3 — Primary and foreign keys
Once the data lives in two tables, we need a reliable way to say "this Codémon belongs to that trainer". Names are not reliable (two trainers could be called Alex), so each table gets an ID column.
A primary key uniquely identifies each row of a table: trainer_id in trainers, codemon_id in codemon. Two rows can never share the same primary key.
A foreign key is a column that stores the primary key of another table. In codemon, the trainer_id column is a foreign key: it points to a row of trainers.
Pyrochu has trainer_id = 2, and trainer number 2 is Morgan: Pyrochu belongs to Morgan. That little number is the whole relationship.
A foreign key can be NULL
Plantachu's trainer_id is NULL: it is a wild Codémon that belongs to nobody. We'll see in the next lesson how to deal with such rows.
Exercise 4 — Keys and their role
Match each concept with its description.
Exercise 5 — The role of trainer_id
In our database, what is the role of the trainer_id column of the codemon table?
Hint : Which table has trainer_id as its primary key?
Exercise 6 — Following a relationship by hand
Let's answer "who owns Shadowchu?" with what we already know. First, read Shadowchu's foreign key:
SELECT name, trainer_id
FROM codemon
WHERE name = 'Shadowchu';Then look up trainer number 2:
SELECT name, hometown
FROM trainers
WHERE trainer_id = 2;Two queries and a mental note in between. It works for one Codémon, but imagine doing it for hundreds — or asking "list every trainer with every Codémon they own".
This is what joins are for
A join does this lookup for every row at once, inside a single query. That's the subject of the next chapter.
Exercise 7 — Why join tables?
You want a report with each Codémon's name next to its trainer's name. Why do you need a join?
Hint : Where is the trainer's name stored? And the Codémon's name?
Exercise 8 — Which table stores it?
Match each piece of information with the table that stores it.
trainerscodemontrainerscodemonExercise 9 — Read the foreign keys
Using the two tables, which Codémons does Taylor own?
Hint : Find Taylor's trainer_id, then look for that number in the codemon table.
Exercise 10 — What kind of relationship?
What is the relationship between trainers and codemon in our database?
Hint : A Codémon row has a single trainer_id. How many Codémon rows can carry the same trainer_id?
Exercise 11 — Before joining
Before the first join, let's make sure you can read a foreign key.
Write a query that returns the name and trainer_id of every Codémon that has a trainer, ordered by trainer_id then by name.
-- Write your query below