๐Ÿ—„๏ธ Database Normalisation

Tidying up data, one step at a time

1๏ธโƒฃ First things first: what is a table?

A database stores information in tables. A table looks like a grid โ€” just like a spreadsheet:

Table: Students
StudentIDFirstNameFormGroup
001Amy10B
002Ben10A
003Cara10B
๐Ÿ’ก Big idea: a database is just a collection of tables that work together.

๐Ÿ”‘ 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.

๐Ÿ’ก A primary key is a column that gives every row its own unique value. It is like your student ID, a passport number, or a barcode. One row = one key. No repeats. Never empty.
Primary key column
Table: Students
๐Ÿ”‘ StudentIDFirstNameLastName
001BenSmith
002BenSmith
003CaraJones

๐Ÿ‘€ See? Two Ben Smiths โ€” but the StudentID tells them apart. That is the whole job of a primary key.

โญ Remember: every table should have a primary key. It's the one column you can trust to be different in every row.

๐Ÿ”— 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!

Primary key Foreign key (a link โžก๏ธ)

Amy's row says F2. Look up F2 in the FormGroups tableโ€ฆ Amy is in 10B with Ms Lopez. ๐ŸŽ‰

๐Ÿ’ก Foreign key = a copy of another table's primary key, used as a link.
๐Ÿ”‘ 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:

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)

๐Ÿ’ก Rule: one value per box.
No lists squashed into a single cell, no repeating columns โ€” and every row needs a primary key.
Problem cells

โŒ NOT in 1NF

Table: Orders (messy!)
CustomerSnacks ordered
AmyCrisps, Cola, Apple
BenCola, Cookie

Three snacks crammed into one cell! The computer can't count them, sort them, or search them properly.

โฌ‡๏ธ fix it โฌ‡๏ธ

โœ… In 1NF

Table: Orders (tidy)
๐Ÿ”‘ OrderLineIDCustomerSnack
1AmyCrisps
2AmyCola
3AmyApple
4BenCola
5BenCookie

Every cell holds exactly one value, and every row has its own key. ๐ŸŽ‰

โญ 1NF in one sentence: one box, one value.

๐Ÿฅˆ Second Normal Form (2NF)

First, be in 1NF. Then apply this rule:

๐Ÿ’ก Rule: every fact must be about the WHOLE key โ€” not just part of it.

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

Table: OrderLines
๐Ÿ”‘ OrderID๐Ÿ”‘ SnackIDQtySnackNameSnackPrice
O1S12Crispsยฃ1.00
O1S21Colaยฃ1.50
O2S13Crispsยฃ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!

โฌ‡๏ธ fix it: move the snack facts to their own table โฌ‡๏ธ

โœ… In 2NF โ€” two tables

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!

โญ 2NF in one sentence: if a fact only belongs to part of the key, move it out to its own table.

๐Ÿฅ‰ Third Normal Form (3NF)

First, be in 2NF. Then apply this rule:

๐Ÿ’ก Rule: facts must be about the key โ€” not about another ordinary column.
No column should depend on a column that isn't the key.

โŒ NOT in 3NF

Table: Students
๐Ÿ”‘ StudentIDNameFormGroupFormTeacher
001Amy10BMs Lopez
002Ben10AMr Khan
003Cara10BMs 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. ๐Ÿ˜ฌ

โฌ‡๏ธ fix it: give form groups their own table โฌ‡๏ธ

โœ… In 3NF โ€” two tables

Each teacher is stored once. If Ms Lopez changes her name, we update one cell. ๐ŸŽ‰

โญ 3NF in one sentence: ordinary columns must describe the key โ€” nothing else.

๐Ÿ The whole story in one box

LevelThe rule (simple version)Fixesโ€ฆ
๐Ÿฅ‡ 1NFOne box, one valueLists squashed into cells
๐Ÿฅˆ 2NFFacts about the whole keyFacts stuck to only part of a two-part key
๐Ÿฅ‰ 3NFFacts about the key, nothing elseFacts that describe another ordinary column
๐Ÿง  The famous memory trick:
Every fact must depend on "the key (1NF), the whole key (2NF), and nothing but the key (3NF)."
๐Ÿ”‘ Primary key โ€” a unique ID for each row.
๐Ÿ”— Foreign key โ€” a link to a primary key in another table.
๐Ÿงน Normalisation โ€” splitting data into linked tables so every fact is stored exactly once.