Database Design and Indexes · Lesson 25 of 30
Database Normalization: 1NF, 2NF and 3NF
Normalization is the process of organising tables so each fact is stored in exactly one place. It prevents contradictory data, saves space and makes changes safe. Every database design interview asks about normal forms – but the idea is easiest to understand by fixing a badly designed table step by step, which is what this lesson does.
In this lesson
- The problems with duplicated data (anomalies)
- First, second and third normal form
- One-to-one, one-to-many and many-to-many relationships
- When denormalizing makes sense
The starting point: one big table
Imagine an order spreadsheet imported straight into a table:
CREATE TABLE orders_flat (
order_id INT,
order_date DATE,
customer_name VARCHAR(60),
customer_email VARCHAR(100),
customer_city VARCHAR(40),
products VARCHAR(200) -- e.g. 'Mouse x1, Pen Pack x2'
);
INSERT INTO orders_flat VALUES
(101, '2025-08-02', 'Asha Verma', 'asha@example.com', 'Pune', 'Wireless Mouse x1, Gel Pen Pack x2'),
(103, '2025-08-19', 'Asha Verma', 'asha@example.com', 'Pune', 'Clean Code x1, SQL in 10 Minutes x1'),
(102, '2025-08-05', 'Ravi Kumar', 'ravi@example.com', 'Delhi', 'Mechanical Keyboard x1');
SELECT order_id, customer_name, customer_city, products FROM orders_flat ORDER BY order_id;
+----------+---------------+---------------+-------------------------------------+ | order_id | customer_name | customer_city | products | +----------+---------------+---------------+-------------------------------------+ | 101 | Asha Verma | Pune | Wireless Mouse x1, Gel Pen Pack x2 | | 102 | Ravi Kumar | Delhi | Mechanical Keyboard x1 | | 103 | Asha Verma | Pune | Clean Code x1, SQL in 10 Minutes x1 | +----------+---------------+---------------+-------------------------------------+
What goes wrong with this design
- Update anomalyAsha moves city: change every one of her rows, or the data contradicts itself
- Insert anomalycan't store a customer until they order
- Delete anomalydelete Ravi's only order and you lose Ravi entirely
- Unqueryable lists"how many pens sold?" means parsing text
First normal form (1NF): atomic values
Each cell holds one value, and there are no repeating groups. The comma-separated products column breaks 1NF. Fix: one row per product in the order.
| order_id | customer_name | product | qty |
|---|---|---|---|
| 101 | Asha Verma | Wireless Mouse | 1 |
| 101 | Asha Verma | Gel Pen Pack | 2 |
| 103 | Asha Verma | Clean Code | 1 |
Now each value is queryable – but customer details are repeated even more.
Second normal form (2NF): no partial dependencies
In the 1NF table the key is (order_id, product). order_date and the customer depend on order_id alone – only part of the key. 2NF says every non-key column must depend on the whole key. Fix: split order-level facts from line-level facts.
Splitting by what each fact depends on
- orders (order_id)order_date, customer details
- order_items (order_id, product)quantity, unit_price
Third normal form (3NF): no transitive dependencies
In orders, the customer's email and city depend on the customer, not on the order: order → customer → city. 3NF says non-key columns must depend on the key, the whole key and nothing but the key. Fix: move customer facts into a customers table and keep only customer_id in orders.
The result is exactly the practice database you've used all course:
-- Asha's city is stored once; every query finds it through the key
SELECT o.order_id, o.order_date, c.name, c.city, p.name AS product, i.quantity
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
JOIN order_items i ON i.order_id = o.order_id
JOIN products p ON p.product_id = i.product_id
WHERE o.order_id IN (101, 103)
ORDER BY o.order_id, product;
+----------+------------+------------+------+-------------------+----------+ | order_id | order_date | name | city | product | quantity | +----------+------------+------------+------+-------------------+----------+ | 101 | 2025-08-02 | Asha Verma | Pune | Gel Pen Pack | 2 | | 101 | 2025-08-02 | Asha Verma | Pune | Wireless Mouse | 1 | | 103 | 2025-08-19 | Asha Verma | Pune | Clean Code | 1 | | 103 | 2025-08-19 | Asha Verma | Pune | SQL in 10 Minutes | 1 | +----------+------------+------------+------+-------------------+----------+
-- The update anomaly is gone: one UPDATE changes Asha's city everywhere
UPDATE customers SET city = 'Mumbai' WHERE customer_id = 1;
SELECT DISTINCT c.name, c.city FROM orders o JOIN customers c ON c.customer_id = o.customer_id WHERE c.customer_id = 1;
+------------+--------+ | name | city | +------------+--------+ | Asha Verma | Mumbai | +------------+--------+
| Normal form | Rule | Fix |
|---|---|---|
| 1NF | atomic values, no repeating groups | one value per cell; one row per item |
| 2NF | 1NF + no column depends on part of a composite key | split by which key part a fact depends on |
| 3NF | 2NF + no column depends on another non-key column | move dependent facts to their own table |
| BCNF | every determinant is a candidate key (a stricter 3NF) | rarely needed beyond 3NF in practice |
Relationships
| Type | Example | How it's modelled |
|---|---|---|
| One-to-many | a customer has many orders | foreign key on the "many" side (orders.customer_id) |
| Many-to-many | orders contain many products; products appear in many orders | junction table (order_items) |
| One-to-one | a user has one profile | foreign key with a UNIQUE constraint, or the same primary key |
When to denormalize
Normalize first, denormalize on purpose
Normalized schemas are right for the systems that record transactions (OLTP). Analytics warehouses deliberately denormalize into wide "star schema" tables to make reports fast, and apps sometimes store a cached total (like orders.total_amount) to avoid summing items on every page view. That's fine when it's a conscious trade-off with a plan to keep the copy in sync.
Practice
Quick check
A column holds "Java, SQL, Python". Which normal form does it break?
1NF – values must be atomic.
A table keyed by (student_id, course_id) stores student_name. What's wrong?
student_name depends on part of the key (student_id only) – a 2NF violation; move it to a students table.
orders stores customer_id and customer_city. Which normal form is broken?
3NF – city depends on the customer, not on the order (a transitive dependency).
How do you model a many-to-many relationship?
With a junction table holding two foreign keys.
Summary
- Duplicated facts cause update, insert and delete anomalies.
- 1NF: atomic values; 2NF: depend on the whole key; 3NF: depend on nothing but the key.
- Model relationships with foreign keys and junction tables; denormalize only deliberately.
Interview practice: normalisation and ER modelling questions.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.