Database Third Normal Form
What you will learn
- The rule of third normal form (3NF)
- What a transitive dependency is
- How to spot one and split it out
- A short way to remember 2NF and 3NF
1. The rule
A table is in 3NF when it is in 2NF and no non-prime column depends on another non-prime column. In the precise form: for every dependency X -> A in the table, either X is a superkey, or A is a prime column (part of some candidate key). The beginner's version is "no non-key column depends on another non-key column". Such an indirect link is a transitive dependency:
emp_id -> dept_id -> dept_name
The employee determines the department, and the department determines its name, so the employee determines the name only through dept_id.
2. An example
| emp_id | name | dept_id | dept_name |
|---|---|---|---|
| 1 | Rina | 10 | Sales |
| 2 | Sam | 10 | Sales |
| 3 | Lee | 20 | Support |
The key is emp_id (a single column), so the table is in 2NF. But dept_id -> dept_name has a left side that is not the key. This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). It adds small tables of its own to your browser's copy; run the examples from top to bottom, and press Reset data to start again. Rename a department in one row, and delete the last employee of a department:
CREATE TABLE staff_bad (emp_id INTEGER PRIMARY KEY, name TEXT, dept_id INTEGER, dept_name TEXT);
INSERT INTO staff_bad VALUES (1, 'Rina', 10, 'Sales'), (2, 'Sam', 10, 'Sales'), (3, 'Lee', 20, 'Support');
UPDATE staff_bad SET dept_name = 'Sales team' WHERE emp_id = 1;
SELECT DISTINCT dept_id, dept_name FROM staff_bad ORDER BY dept_id, dept_name;
dept_id dept_name
------- ----------
10 Sales
10 Sales team
20 Support
(3 rows)
DELETE FROM staff_bad WHERE emp_id = 3;
SELECT COUNT(*) AS employees_in_dept_20 FROM staff_bad WHERE dept_id = 20;
employees_in_dept_20
--------------------
0
(1 row)
CREATE TABLE staff_bad (emp_id INTEGER PRIMARY KEY, name TEXT, dept_id INTEGER, dept_name TEXT);
INSERT INTO staff_bad VALUES (1, 'Rina', 10, 'Sales'), (2, 'Sam', 10, 'Sales'), (3, 'Lee', 20, 'Support');
UPDATE staff_bad SET dept_name = 'Sales team' WHERE emp_id = 1;
SELECT DISTINCT dept_id, dept_name FROM staff_bad ORDER BY dept_id, dept_name;
dept_id dept_name
------- ----------
10 Sales
10 Sales team
20 Support
(3 rows)
DELETE FROM staff_bad WHERE emp_id = 3;
SELECT COUNT(*) AS employees_in_dept_20 FROM staff_bad WHERE dept_id = 20;
employees_in_dept_20
--------------------
0
(1 row)
CREATE TABLE staff_bad (emp_id INTEGER PRIMARY KEY, name TEXT, dept_id INTEGER, dept_name TEXT);
INSERT INTO staff_bad VALUES (1, 'Rina', 10, 'Sales'), (2, 'Sam', 10, 'Sales'), (3, 'Lee', 20, 'Support');
UPDATE staff_bad SET dept_name = 'Sales team' WHERE emp_id = 1;
SELECT DISTINCT dept_id, dept_name FROM staff_bad ORDER BY dept_id, dept_name;
dept_id dept_name
------- ----------
10 Sales
10 Sales team
20 Support
(3 rows)
DELETE FROM staff_bad WHERE emp_id = 3;
SELECT COUNT(*) AS employees_in_dept_20 FROM staff_bad WHERE dept_id = 20;
employees_in_dept_20
--------------------
0
(1 row)
Department 10 now has two names (update anomaly), and the Support department disappeared with its only employee (delete anomaly).
3. The fix
Move the dependent column to a table keyed by the column it depends on:
| dept_id | dept_name |
|---|---|
| 10 | Sales |
| 20 | Support |
| emp_id | name | dept_id |
|---|---|---|
| 1 | Rina | 10 |
| 2 | Sam | 10 |
| 3 | Lee | 20 |
CREATE TABLE dept_list (dept_id INTEGER PRIMARY KEY, dept_name TEXT);
CREATE TABLE staff_good (emp_id INTEGER PRIMARY KEY, name TEXT, dept_id INTEGER REFERENCES dept_list (dept_id));
INSERT INTO dept_list VALUES (10, 'Sales'), (20, 'Support');
INSERT INTO staff_good VALUES (1, 'Rina', 10), (2, 'Sam', 10), (3, 'Lee', 20);
DELETE FROM staff_good WHERE emp_id = 3;
SELECT dept_name FROM dept_list ORDER BY dept_id;
dept_name
---------
Sales
Support
(2 rows)
SELECT e.name, d.dept_name
FROM staff_good e JOIN dept_list d USING (dept_id)
ORDER BY e.emp_id;
name dept_name
---- ---------
Rina Sales
Sam Sales
(2 rows)
CREATE TABLE dept_list (dept_id INTEGER PRIMARY KEY, dept_name TEXT);
CREATE TABLE staff_good (emp_id INTEGER PRIMARY KEY, name TEXT, dept_id INTEGER REFERENCES dept_list (dept_id));
INSERT INTO dept_list VALUES (10, 'Sales'), (20, 'Support');
INSERT INTO staff_good VALUES (1, 'Rina', 10), (2, 'Sam', 10), (3, 'Lee', 20);
DELETE FROM staff_good WHERE emp_id = 3;
SELECT dept_name FROM dept_list ORDER BY dept_id;
dept_name
---------
Sales
Support
(2 rows)
SELECT e.name, d.dept_name
FROM staff_good e JOIN dept_list d USING (dept_id)
ORDER BY e.emp_id;
name dept_name
---- ---------
Rina Sales
Sam Sales
(2 rows)
CREATE TABLE dept_list (dept_id INTEGER PRIMARY KEY, dept_name TEXT);
CREATE TABLE staff_good (emp_id INTEGER PRIMARY KEY, name TEXT, dept_id INTEGER REFERENCES dept_list (dept_id));
INSERT INTO dept_list VALUES (10, 'Sales'), (20, 'Support');
INSERT INTO staff_good VALUES (1, 'Rina', 10), (2, 'Sam', 10), (3, 'Lee', 20);
DELETE FROM staff_good WHERE emp_id = 3;
SELECT dept_name FROM dept_list ORDER BY dept_id;
dept_name
---------
Sales
Support
(2 rows)
SELECT e.name, d.dept_name
FROM staff_good e JOIN dept_list d USING (dept_id)
ORDER BY e.emp_id;
name dept_name
---- ---------
Rina Sales
Sam Sales
(2 rows)
dept_id stays in the staff table as a foreign key, so nothing is lost: a join gives back the original view. The department name is stored once.
The sample databases keep such facts apart too. A department name or a branch name is stored once and referred to by id:
SELECT p.product_id, p.category_id, c.name AS category
FROM products p JOIN categories c ON c.category_id = p.category_id
ORDER BY p.product_id LIMIT 3;
product_id category_id category
---------- ----------- --------
1 2 Kitchen
2 2 Kitchen
3 2 Kitchen
(3 rows)
SELECT a.account_id, a.branch_id, b.name AS branch
FROM accounts a JOIN branches b ON b.branch_id = a.branch_id
ORDER BY a.account_id LIMIT 3;
account_id branch_id branch
---------- --------- ---------
1 1 Central
2 2 Riverside
3 5 Airport
(3 rows)
SELECT t.teacher_id, t.department_id, d.name AS department
FROM teachers t JOIN departments d ON d.department_id = t.department_id
ORDER BY t.teacher_id LIMIT 3;
teacher_id department_id department
---------- ------------- ----------
1 6 Computing
2 3 Languages
3 2 Science
(3 rows)
4. A way to remember
A saying often used as a memory aid for 1NF to 3NF is that every non-key column must depend on the key, the whole key and nothing but the key. It is a simplification, because it ignores the prime-column exception that lesson 5.6 explains:
| Part | Normal form |
|---|---|
| the key | 1NF: a key exists and cells are atomic |
| the whole key | 2NF: no partial dependencies |
| nothing but the key | 3NF: no transitive dependencies |
Worked example: find the transitive dependency
students (student_id, name, class_id, class_teacher, teacher_phone), key student_id.
class_id -> class_teacher: left side not the key. Transitive.class_teacher -> teacher_phone: also not a key. Transitive.
Split: classes (class_id, class_teacher_id), teachers (teacher_id, name, phone), students (student_id, name, class_id). Now each fact has one home. Names are made up.
Common mistakes
- Stopping at 2NF because the key is a single column. 2NF says nothing about non-key columns depending on each other.
- Moving the column but leaving
dept_idout ofemployees. The foreign key is the link. - Treating every derived-looking column as a violation. Check the real-world rule first (lesson 5.2).
Key takeaways
- 3NF = 2NF + no non-key column depends on another non-key column.
- Transitive chain:
key -> A -> B; split A and B into their own table. - Keep A in the original table as a foreign key.
- Remember: the key, the whole key, nothing but the key.
Practice
1. What is a transitive dependency?
Show answer
A non-key column that depends on the key only through another non-key column (key -> A -> B).
2. In employees (emp_id, name, dept_id, dept_name), which dependency breaks 3NF?
Show answer
dept_id -> dept_name, since dept_id is not the key.
3. What was lost in the original table when Lee was deleted?
Show answer
The Support department, because its only copy was in Lee's row.
4. After the split, how do you list employees with department names?
Show answer
Join the tables on dept_id.
5. Which normal form covers "the whole key"?
Show answer
2NF.
6. Split books (book_id, title, publisher_id, publisher_name, publisher_city).
Show answer
publishers (publisher_id, publisher_name, publisher_city) and books (book_id, title, publisher_id).