1๏ธโฃ First things first: what is a table?
A database stores information in tables. A table looks like a grid โ just like a spreadsheet:
- โก๏ธ Each row is one thing (one student, one pizza, one film).
- โฌ๏ธ Each column is one piece of information about it (name, price, date).
| StudentID | FirstName | FormGroup |
|---|---|---|
| 001 | Amy | 10B |
| 002 | Ben | 10A |
| 003 | Cara | 10B |
๐ The Primary Key
Imagine two students both called Ben Smith. How does the school tell them apart?
Easy โ every student gets a unique ID number. No two students can ever have the same one.
| ๐ StudentID | FirstName | LastName |
|---|---|---|
| 001 | Ben | Smith |
| 002 | Ben | Smith |
| 003 | Cara | Jones |
๐ See? Two Ben Smiths โ but the StudentID tells them apart. That is the whole job of a primary key.
๐ The Foreign Key
Databases usually have more than one table. So how do tables talk to each other?
They use a foreign key โ a column in one table that points to the primary key of another table. It's like writing someone's phone number in your contacts: you don't copy the whole person, you just keep a link to them!
| ๐ FormID | FormName | Teacher |
|---|---|---|
| F1 | 10A | Mr Khan |
| F2 | 10B | Ms Lopez |
| ๐ StudentID | FirstName | ๐ FormID |
|---|---|---|
| 001 | Amy | F2 |
| 002 | Ben | F1 |
| 003 | Cara | F2 |
Amy's row says F2. Look up F2 in the FormGroups tableโฆ Amy is in 10B with Ms Lopez. ๐
๐ Primary key: "this is ME."
๐ Foreign key: "I belong to THAT."
๐งน What is normalisation? (and why bother?)
Normalisation means tidying up a database step by step so that:
- โ Each fact is stored once (no copying and pasting the same data everywhere)
- โ Nothing gets out of date or contradicts itself
- โ Tables are small, neat and easy to update
Think of a messy wardrobe ๐งฆ๐๐ with everything in one big pile. Normalisation is like sorting it into drawers: socks here, shirts there. Same stuff โ much easier to find and manage.
๐ฑ Why messy data is a problem
If "Ms Lopez" is typed 50 times and she changes her name, you must fix it 50 times. Miss one, and your database now lies to you.
The tidy-up happens in three steps, called normal forms: 1NF โก๏ธ 2NF โก๏ธ 3NF. Each step builds on the one before, like levels in a game. ๐ฎ
๐ฅ First Normal Form (1NF)
No lists squashed into a single cell, no repeating columns โ and every row needs a primary key.
โ NOT in 1NF
| Customer | Snacks ordered |
|---|---|
| Amy | Crisps, Cola, Apple |
| Ben | Cola, Cookie |
Three snacks crammed into one cell! The computer can't count them, sort them, or search them properly.
โ In 1NF
| ๐ OrderLineID | Customer | Snack |
|---|---|---|
| 1 | Amy | Crisps |
| 2 | Amy | Cola |
| 3 | Amy | Apple |
| 4 | Ben | Cola |
| 5 | Ben | Cookie |
Every cell holds exactly one value, and every row has its own key. ๐
๐ฅ Second Normal Form (2NF)
First, be in 1NF. Then apply this rule:
This matters when the key is made of two columns together (a "composite key"). Look at this table โ its key is OrderID + SnackID together:
โ NOT in 2NF
| ๐ OrderID | ๐ SnackID | Qty | SnackName | SnackPrice |
|---|---|---|---|---|
| O1 | S1 | 2 | Crisps | ยฃ1.00 |
| O1 | S2 | 1 | Cola | ยฃ1.50 |
| O2 | S1 | 3 | Crisps | ยฃ1.00 |
๐ต๏ธ Qty depends on the whole key (this snack in this order) โ fine.
But SnackName and SnackPrice only depend on SnackID โ just part of the key. That's why "Crisps ยฃ1.00" is written twice!
โ In 2NF โ two tables
| ๐ OrderID | ๐๐ SnackID | Qty |
|---|---|---|
| O1 | S1 | 2 |
| O1 | S2 | 1 |
| O2 | S1 | 3 |
| ๐ SnackID | SnackName | SnackPrice |
|---|---|---|
| S1 | Crisps | ยฃ1.00 |
| S2 | Cola | ยฃ1.50 |
Now "Crisps ยฃ1.00" is stored once. Change the price in one place and every order sees it. ๐ Notice how SnackID became a ๐ foreign key linking the tables!
๐ฅ Third Normal Form (3NF)
First, be in 2NF. Then apply this rule:
No column should depend on a column that isn't the key.
โ NOT in 3NF
| ๐ StudentID | Name | FormGroup | FormTeacher |
|---|---|---|---|
| 001 | Amy | 10B | Ms Lopez |
| 002 | Ben | 10A | Mr Khan |
| 003 | Cara | 10B | Ms Lopez |
๐ต๏ธ FormTeacher doesn't really depend on the student โ it depends on FormGroup (every 10B student has Ms Lopez). A fact about an ordinary column, not the key! So "Ms Lopez" is repeated for every 10B student. ๐ฌ
โ In 3NF โ two tables
| ๐ StudentID | Name | ๐ FormGroup |
|---|---|---|
| 001 | Amy | 10B |
| 002 | Ben | 10A |
| 003 | Cara | 10B |
| ๐ FormGroup | FormTeacher |
|---|---|
| 10A | Mr Khan |
| 10B | Ms Lopez |
Each teacher is stored once. If Ms Lopez changes her name, we update one cell. ๐
๐ The whole story in one box
| Level | The rule (simple version) | Fixesโฆ |
|---|---|---|
| ๐ฅ 1NF | One box, one value | Lists squashed into cells |
| ๐ฅ 2NF | Facts about the whole key | Facts stuck to only part of a two-part key |
| ๐ฅ 3NF | Facts about the key, nothing else | Facts that describe another ordinary column |
Every fact must depend on "the key (1NF), the whole key (2NF), and nothing but the key (3NF)."
๐ Foreign key โ a link to a primary key in another table.
๐งน Normalisation โ splitting data into linked tables so every fact is stored exactly once.