Third Normal Form (3NF) — Database

3NF (Third Normal Form, the third rule of normalisation) = already 2NF (no partial dependency, a column that depends on only part of the key), and no…

On Sunset Coffee's menu board, the category name is written next to every item: "Milk Coffee, category Coffee", "Palm Sugar Milk Coffee, category Coffee". The board is the products table: one line on the board is one row in the table, and the category name next to an item is the category_name column. One day the owner wants to rename the "Coffee" category. She has to rewrite the name next to every coffee on the board. If she misses one, the board contradicts itself.

The category name really belongs to the category, not to the item. This is the problem that Third Normal Form (3NF), the third rule of normalisation, prevents: a non-key column must not depend on another non-key column. Every column has to describe its own row, not its neighbouring column.

The table we use

After 2NF, Sunset Coffee has a products table with a one-column primary key, id. A partial dependency is no longer possible, because a one-column key has no "part". But look at its last two columns:

id name price category_id category_name
1 Milk Coffee 450 1 Coffee
2 Palm Sugar Milk Coffee 550 1 Coffee
3 Milk Tea 375 2 Non-Coffee
4 Toast 500 3 Food

A chain of dependencies: transitive dependency

Follow which column determines which. Read the arrow -> below as "determines": the right column depends on the left column.

id  ->  category_id  ->  category_name
 1  ->       1       ->  Coffee

This chained dependency is called a transitive dependency: a non-key column depends on another non-key column, and only reaches the primary key through it. 3NF has two conditions: the table already meets 2NF, and there is no transitive dependency. The products table above breaks 3NF because of category_name.

Budi's phone number problem in the What Is Database Design lesson has the same shape. In the do-everything orders table, customer_phone depends on the customer, not on the order's id.

One category, two names

The owner renames category 1 to Archipelago Coffee, but this UPDATE changes only the Palm Sugar Milk Coffee row (id 2). Then SELECT DISTINCT shows each different pair of category_id and category_name:

UPDATE products SET category_name = 'Archipelago Coffee' WHERE id = 2;

SELECT DISTINCT category_id, category_name
FROM products
ORDER BY category_id;
category_id category_name
1 Coffee
1 Archipelago Coffee
2 Non-Coffee
3 Food

Category 1 now has two names, and the database doesn't refuse it. This is an update anomaly, the same as in the Second Normal Form (2NF) lesson, but here the cause is a transitive dependency. Two problems of the same kind also appear:

You can change every row of category 1 at the same time with WHERE category_id = 1. But one fact, the name of category 1, is still stored in 2 rows. With 40 coffees on the menu, it is 40 rows.

Move it to its own table

category_name, which depends on category_id, moves to a new table, categories, as the column name. Its primary key (id) holds the category number, the same value as category_id:

CREATE TABLE categories (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);

CREATE TABLE products (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  price INTEGER NOT NULL,
  category_id INTEGER REFERENCES categories(id)  -- foreign key
);

INSERT INTO categories VALUES (1, 'Coffee'), (2, 'Non-Coffee'), (3, 'Food');
INSERT INTO products VALUES (1, 'Milk Coffee', 450, 1), (2, 'Palm Sugar Milk Coffee', 550, 1),
  (3, 'Milk Tea', 375, 2), (4, 'Toast', 500, 3);

Now a rename changes only one row in categories. This query renames category 1, then shows the items of that category through a JOIN:

UPDATE categories SET name = 'Archipelago Coffee' WHERE id = 1;

SELECT p.name, c.name AS category_name
FROM products p
JOIN categories c ON c.id = p.category_id
WHERE p.category_id = 1;
name category_name
Milk Coffee Archipelago Coffee
Palm Sugar Milk Coffee Archipelago Coffee

Only one row changed, and every coffee now shows the new name. If Toast is deleted, the Food category is still in categories. Snacks can be added any time, even with no items yet.

The same pattern appears in an employees (id, name, department_id, department_name) table: department_name depends on department_id, not on id. The fix is a separate departments table, and employees keeps only department_id as a foreign key.

1NF, 2NF, 3NF in one table

The problem Breaks The fix
multi-value: one cell holds several values, or an array in a column ("07700 900111, 07700 900555", ["a","b"]) 1NF one value per cell, the rest become rows in another table
partial dependency: depends on part of a composite key 2NF move it to the table that belongs to that part of the key
transitive dependency: a non-key depends on a non-key 3NF move it to the table of the column that determines it
none of the three already normal

Higher normal forms (BCNF, 4NF, 5NF) exist, but most apps in real use stop at 3NF.

⚠️ Move it, don't delete it

TL;DR: 3NF (Third Normal Form, the third rule of normalisation) = already 2NF (no partial dependency, a column that depends on only part of the key), and no transitive dependency. A transitive dependency means a non-key column (a column outside the primary key) depends on another non-key column. In products (id, name, price, category_id, category_name) the chain is id -> category_id -> category_name: id determines the product's category, and category_id determines the category's name. So renaming the category on only one row leaves one category with two names. The fix: a categories table of its own, and products keeps category_id as a foreign key (a column that points at the id in another table). The same pattern moves department_name from employees to departments. In order: multi-value (several values in one cell) or an array in a column breaks 1NF, a partial dependency breaks 2NF, a transitive dependency breaks 3NF.

⚡ A column that describes its neighbour

Write:

id  ->  category_id  ->  category_name
 1  ->       1       ->  Coffee
Guess first: in the products table, the arrow -> means "determines": product 1 is in category 1, and category 1 is always Coffee. So does category_name depend on id directly, or through another column?

Through category_id, which isn't a key. A chain like that is called a transitive dependency, and 3NF removes it.

Meaning: 3NF (Third Normal Form) is the third rule of normalisation. The table must already meet 2NF (the second rule), and a non-key column (a column outside the primary key) must not depend on another non-key column.

⚡ One category, two names

Write:

UPDATE products SET category_name = 'Archipelago Coffee' WHERE id = 2;

Meaning: The category is renamed on one row only, the Palm Sugar Milk Coffee row (id 2). Milk Coffee is also in category 1 and still says Coffee. So category 1 now has two names, Coffee and Archipelago Coffee, and the database doesn't refuse it.

⚡ Move it, don't delete it

Write:

CREATE TABLE categories (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);

Meaning: category_name isn't thrown away, it moves. The category name is stored once in a new table, categories. products only keeps category_id as a foreign key (a column that points at the id in categories). In order: a list in one cell breaks 1NF, a partial dependency (depending on part of the key) breaks 2NF, a transitive dependency breaks 3NF.

In the exercise, your function returns exact text such as "breaks 3NF", and "already normal" for any other problem.

What you will learn