Sql - Table relationships
📖 Read the lesson
Code Life
By Pierre Marsaa
Why several tables?
Imagine the league kept everything in
one big table
: each Codémon with its trainer's name, hometown and badges.
codemon
type
trainer
hometown
badges
Aquachu
water
Alex
Sunset Valley
3
Voltachu
electric
Alex
Sunset Valley
3
Pyrochu
fire
Morgan
Azure City
5
Shadowchu
ghost
Morgan
Azure City
5
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.
Loading data...
Loading data...
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
.
Verify