2. Designing Tables & Combining Data
Database Normalization (1NF, 2NF & 3NF Made Simple)
Learn how to organize tables cleanly without duplicate data bugs: understand 1st, 2nd, and 3rd Normal Form in plain English using real before-and-after table examples.
- Spot duplicate-data traps in poorly designed tables before writing code
- Apply 1st, 2nd, and 3rd Normal Form (1NF–3NF) in plain English
- Split messy flat spreadsheets into clean, linked relational tables
Imagine starting an online store and saving every order inside one giant spreadsheet where every single row repeats the customer's name, city, and purchased items:
| order_id | customer_name | customer_city | items |
|---|---|---|---|
| 1 | Alex Rivera | Chicago | Keyboard, Lamp |
| 2 | Alex Rivera | Chicago | Python Book |
At 10 rows, that spreadsheet feels convenient. At 10,000 rows, it becomes a maintenance nightmare. What happens when Alex moves from Chicago to Seattle? You have to hunt down and update every past order row—and if a single row fails to update, your database contradicts itself. Worse, trying to count how many Lamps were sold means parsing messy comma-separated text.
Database normalization sounds academic, but it is really just a practical three-part checklist that stops duplicate-data bugs by ensuring every fact lives in one place.
Before (Denormalized / Repeated)
Email & name duplicated on every row—easy to go out of sync.
After (Normalized 3NF Tables)
Splitting repeated customer/course info into separate tables prevents update anomalies: changing Maya's email happens in ONE row instead of 50 order rows.
First Normal Form (1NF): One Value Per Cell
Look closely at Order #1 above: the items column squeezes two different products into a single cell ("Keyboard, Lamp"). You cannot easily sort by product, join against an inventory price list, or calculate how many Keyboards were ordered last month.
The 1NF Rule: Keep every cell atomic—store one value per cell, never comma-separated lists. Give each purchased item its own row inside an order_items table:
| order_id | item_name |
|---|---|
| 1 | Keyboard |
| 1 | Lamp |
| 2 | Python Book |
Notice how clean filtering and counting immediately become once each cell holds a single value.
Second Normal Form (2NF): Belong to the Whole Key
Suppose our order_items table uses a combined primary key of (order_id, product_id) and stores both quantity and product_price:
| order_id | product_id | quantity | product_name |
|---|---|---|---|
| 1 | 101 | 2 | Mechanical Keyboard |
| 2 | 101 | 1 | Mechanical Keyboard |
Look at what depends on what: quantity genuinely depends on both which order and which product ((order_id, product_id)). However, product_name depends only on product_id—it does not care which order it sits on! Repeating "Mechanical Keyboard" across thousands of order lines wastes space and causes drift if the product is renamed.
The 2NF Rule: Every non-key column must depend on the entire primary key, not just half of it. Move product attributes (product_name, unit_price) into their own products table, and keep only product_id and quantity inside order_items.
Third Normal Form (3NF): No Indirect Side Dependencies
Now imagine an employees table that tracks which department someone works in along with the department's shared office phone number:
| employee_id | full_name | department_id | department_phone |
|---|---|---|---|
| 11 | Maya Chen | 4 | 555-0140 |
| 12 | Omar Farooq | 4 | 555-0140 |
Here, department_phone describes the department, not the individual employee. It sneaked into employees indirectly via department_id. If Engineering gets a new phone line, you would have to update dozens of employee rows instead of one.
The 3NF Rule: A non-key column should never describe another non-key column. Move department_phone into a departments table so changing it edits exactly one row.
Every non-key column in a table must describe the key (1NF), the whole key (2NF), and nothing but the key (3NF)!
Try It Yourself: Querying Normalized 3NF Tables
Our live sample database splits customers, orders, order_items, and products cleanly—letting you join them together without any duplicated customer names inside order_items:
Click “Run Code” to execute this snippet right in your browser.
Quick Checkpoints
Which normalization rule does storing 'Python, SQL, Git' inside a single skills text cell violate?