Database Normalization¶
What Is Normalization?¶
Imagine you have a messy backpack.
You have pencils everywhere, papers everywhere, and sometimes you have the same thing in three different places.
A database can become messy in the same way.
Normalization is the process of organizing a database so that:
We don’t store the same information over and over.
Our data is easier to change.
Our data is less likely to have mistakes.
Each piece of information has a good place to live.
Think of normalization as:
“Clean up the database and put everything where it belongs.”
The Three Main Steps¶
The three normal forms we usually learn first are:
1NF → 2NF → 3NF
Each step makes the database more organized.
You can think of it like cleaning your bedroom:
- Messy Room
v
- Put everything in groups
v
- Give everything its own place
v
Keep everything organized
1NF - First Normal Form¶
1NF is the first step.
The main rule is:
Each box in a table should contain only ONE piece of information.
For example, imagine we have this table:
Student |
Grade |
Favorite Foods |
|---|---|---|
Alex |
5 |
Pizza, Tacos, Apples |
The problem is that the “Favorite Foods” box contains THREE things.
That’s messy.
We want each box to contain just one thing.
So we can change it to:
Student |
Grade |
Favorite Food |
|---|---|---|
Alex |
5 |
Pizza |
Alex |
5 |
Tacos |
Alex |
5 |
Apples |
Now every box contains one value.
That’s 1NF.
Note
Easy way to remember 1NF:
ONE box = ONE thing
2NF - Second Normal Form¶
2NF builds on 1NF.
The big idea is:
Don’t put information in a table if it only belongs to part of the key.
This sounds complicated, so let’s use an example.
Imagine a school has this table:
Student ID |
Class ID |
Student Name |
Grade |
|---|---|---|---|
101 |
10 |
Alex |
A |
101 |
20 |
Alex |
B |
102 |
10 |
Sarah |
A |
The key is:
Student ID + Class ID
Together, these tell us exactly which student’s class record we are looking at.
But look at “Student Name.”
Student Name only depends on Student ID.
It does not depend on Class ID.
So we are storing Alex’s name multiple times.
Instead, we can split the information into two tables.
Student Table:
Student ID |
Student Name |
|---|---|
101 |
Alex |
102 |
Sarah |
Class Enrollment Table:
Student ID |
Class ID |
Grade |
|---|---|---|
101 |
10 |
A |
101 |
20 |
B |
102 |
10 |
A |
Now the student’s name is stored in one place.
That’s 2NF.
Note
Easy way to remember 2NF:
Information should depend on the WHOLE key.
Don’t put something in a table if it only belongs to PART of the key.
3NF - Third Normal Form¶
3NF takes things one step further.
The main idea is:
A piece of information should depend directly on the key, not on another piece of information.
That sounds scary, but here’s an easy example.
Imagine we have:
Student ID |
Student Name |
School ID |
|---|---|---|
101 |
Alex |
5 |
102 |
Sarah |
7 |
Now imagine we add the school name:
The problem is that School Name depends on School ID, not Student ID.
The relationship is:
- Student ID
v
- School ID
v
School Name
School Name is indirectly connected to Student ID.
That’s something we want to avoid.
Instead, we can make a separate School table.
Students:
Student ID |
Student Name |
School ID |
|---|---|---|
101 |
Alex |
5 |
102 |
Sarah |
7 |
Schools:
School ID |
School Name |
|---|---|
5 |
Lincoln School |
7 |
Washington High |
Now each piece of information has one good place to live.
That’s 3NF.
Note
Easy way to remember 3NF:
Information should depend on the key, and NOT on another non-key piece of information.
The Whole Idea¶
Here is the easiest way to remember all three:
1NF
One box = one thing.
2NF
Everything depends on the WHOLE key.
3NF
Everything depends DIRECTLY on the key.
You can remember it like this:
1NF | +– One thing in each box
v
2NF | +– Depends on the whole key
v
3NF | +– Depends directly on the key
Why Do We Normalize?¶
Normalization helps prevent problems.
Imagine you have Alex’s school name written in 50 different rows.
Then Alex’s school changes its name.
You would have to change all 50 rows.
If you forget one, your database could say:
Alex → Lincoln School
and somewhere else:
Alex → Washington High
Now the database has conflicting information!
Normalization helps us avoid this by storing information in the right place.
Instead of:
Alex → Lincoln School Alex → Lincoln School Alex → Lincoln School Alex → Lincoln School Alex → Lincoln School
we can store:
Alex → School 5
School 5 → Lincoln School
Now we only need to change the school name in ONE place.
Quick Review¶
Form |
Easy Explanation |
|---|---|
1NF |
One box should contain one thing. |
2NF |
Information depends on the whole key. |
3NF |
Information depends directly on the key. |
The Big Picture¶
Normalization is basically:
- Take a messy table
v
- Break it into smaller tables
v
- **Put each piece of information
- where it belongs**
v
- Connect the tables using keys
v
Get a cleaner database!
The Most Important Thing to Remember¶
Don’t worry about memorizing the complicated definitions at first.
Remember this:
1NF = ONE
One thing in each box.
2NF = WHOLE
Information depends on the whole key.
3NF = DIRECT
Information depends directly on the key.
So:
1NF → ONE 2NF → WHOLE 3NF → DIRECT
That’s the basic idea of database normalization.