Table relationships

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 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?

Options
• The trainer's information is repeated on every one of their Codémons, so an update must be made in several places
• Queries become impossible to write
• A table cannot have more than four columns
• SQL does not allow text and numbers in the same table

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.

Items to match
• Primary key
• Foreign key
• One-to-many
• NULL foreign key
Definitions
• Uniquely identifies each row of a table
• Stores the primary key of a row in another table
• One trainer can own many Codémons, each Codémon has at most one trainer
• A row that is linked to nothing, like a wild Codémon

Exercise 5 — The role of trainer_id

In our database, what is the role of the trainer_id column of the codemon table?

Options
• It is a foreign key: it points to the trainer who owns the Codémon
• It is the primary key of the codemon table
• It is a copy of the trainer's name
• It counts how many trainers the Codémon has had

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?

Options
• The two names live in two different tables, and a join combines their rows in one query
• Every SQL query needs a join, even on a single table
• Because the codemon table has no name column
• A join is the only way to sort results

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.

Items to match
• The trainer's hometown
• The Codémon's type
• The number of badges
• The capture date
Definitions
• trainers
• codemon
• trainers
• codemon

Exercise 9 — Read the foreign keys

Using the two tables, which Codémons does Taylor own?

Options
• Florachu, Rockachu and Firochu
• Aquachu and Voltachu
• Pyrochu and Shadowchu
• Plantachu only

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?

Options
• One-to-many: a trainer can own several Codémons, but a Codémon has at most one trainer
• One-to-one: each trainer owns exactly one Codémon
• Many-to-many: a Codémon can belong to several trainers at once
• No relationship: the tables are independent

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.

Starter code
-- Write your query below
Back to the course