The First Normal Form (1NF) lesson ended with order_items still storing product names as text. Milk Coffee is written in order 1, then written again in order 3. It looks harmless, until the owner wants to rename a menu item.
Picture Sunset Coffee's order book on paper. Each line in the book is one menu item on one receipt, say "receipt 1, Milk Coffee, 2 cups". The book is the order_items table: the receipt number is the order number, and one line in the book is one row in the table. The number of cups really belongs to that line. But the name "Milk Coffee" doesn't belong to the receipt, it belongs to the menu item. If the item name is written again on every receipt, renaming it means turning every page, and it is easy to miss one.
The second rule of normalisation, Second Normal Form (2NF), prevents that problem. In a table whose primary key is made of two columns, every other column has to depend on both of them, not just one.
The table we use
In the 1NF lesson, Sunset Coffee's order contents moved to order_items, one row per product. Now each row also records the product_id and the quantity (qty):
| order_id | product_id | product_name | qty |
|---|---|---|---|
| 1 | 1 | Milk Coffee | 2 |
| 1 | 4 | Toast | 1 |
| 2 | 2 | Palm Sugar Milk Coffee | 1 |
| 3 | 3 | Milk Tea | 1 |
| 3 | 1 | Milk Coffee | 1 |
Orders 1 and 2 are Budi's, order 3 is Siti's. The primary key is (order_id, product_id). A primary key made of two or more columns is called a composite primary key (a combined primary key). What has to be unique is the pair: order_id 1 may appear twice, but the pair (1, 1) may appear only once. The columns that aren't part of the primary key (product_name and qty) are called non-key columns.
What does it depend on?
Column B depends on column A when, once you know the value of A, there is exactly one possible value of B. The formal name is functional dependency. Check the two non-key columns:
| Column | Knowing this is enough... | Kind |
|---|---|---|
product_name |
product_id alone: product 1 is always Milk Coffee, in any order |
partial dependency |
qty |
order_id and product_id: 2 Milk Coffees in order 1, but 1 in order 3 |
full dependency |
A partial dependency means a non-key column depends on only part of the primary key. 2NF has two conditions: the table already meets 1NF, and every non-key column depends fully on the whole primary key. Strictly, the second condition holds for every candidate key: each column or set of columns that is unique per row and could be chosen as the primary key. A column that is part of any candidate key is not a non-key column. So the order_items above breaks 2NF because of product_name.
Change one row, and the data contradicts itself
The owner renames Milk Coffee through order 1. This UPDATE changes only the row of order 1, then SELECT shows every Milk Coffee row (product 1):
UPDATE order_items SET product_name = 'Sunset Milk Coffee'
WHERE order_id = 1 AND product_id = 1;
SELECT order_id, product_id, product_name
FROM order_items
WHERE product_id = 1;
Before you look at the result, guess: does Milk Coffee now come out with one name or two?
| order_id | product_id | product_name |
|---|---|---|
| 1 | 1 | Sunset Milk Coffee |
| 3 | 1 | Milk Coffee |
The answer is two: the same product now has two names, and the database doesn't refuse it. This problem is called an update anomaly: the data goes wrong when it is changed, with no error message. There are two problems of the same kind:
- Delete anomaly: order 2 is cancelled (
DELETE FROM order_items WHERE order_id = 2;). Palm Sugar Milk Coffee only appeared in that order, so the shop's data "forgets" Palm Sugar Milk Coffee is on the menu. - Insert anomaly: a new item, Fried Banana, hasn't been ordered yet. Fried Banana has nowhere to go in this table, because every row needs an
order_id.
Split the table
The column that depends on product_id alone moves to the products table, whose primary key (id) is the product's number itself:
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL REFERENCES products(id),
qty INTEGER NOT NULL,
PRIMARY KEY (order_id, product_id) -- composite primary key
);
INSERT INTO products VALUES (1, 'Milk Coffee'), (2, 'Palm Sugar Milk Coffee'), (3, 'Milk Tea'), (4, 'Toast');
INSERT INTO order_items VALUES (1, 1, 2), (1, 4, 1), (2, 2, 1), (3, 3, 1), (3, 1, 1);
productsstores each product's name once. Fried Banana can simply be added here, before anyone orders it.order_itemskeeps only what really belongs to the pair of order and product:qty.REFERENCES products(id)linksproduct_idtoproducts. The details are in the Primary Key & Foreign Key lesson.NOT NULLis written on both key columns, because SQLite allowsNULLin a composite primary key unless you writeNOT NULL. MySQL and PostgreSQL don't allow it.
Now a rename changes one row in products, and every order reads that name through a JOIN:
UPDATE products SET name = 'Sunset Milk Coffee' WHERE id = 1;
SELECT oi.order_id, p.name AS product_name, oi.qty
FROM order_items oi
JOIN products p ON p.id = oi.product_id
WHERE oi.product_id = 1;
| order_id | product_name | qty |
|---|---|---|
| 1 | Sunset Milk Coffee | 2 |
| 3 | Sunset Milk Coffee | 1 |
The name of Milk Coffee can't contradict itself any more, because the name lives in one place. And if order 2 is cancelled, Palm Sugar Milk Coffee is still in products.
When 2NF needs checking
A partial dependency is only possible when some key is composite. If every candidate key is a single column (such as an auto-increment id, a number that goes up by itself), there is no "part" of that key. So a table that meets 1NF meets 2NF automatically. You can calculate it with two numbers: the columns in the primary key, and the key columns a column leans on (depends on):
| Columns in the primary key | Key columns it leans on | Result |
|---|---|---|
| 2 | 1 (product_name on product_id) |
partial dependency |
| 2 | 2 (qty on both) |
full dependency |
| 1 | 1 (name on products.id) |
no partial possible |
| 3 | 2 | partial dependency |
⚠️ Which columns move, and which stay?
- Hunting for a partial dependency in a table with a single
id. As long asidis the only key, there can't be a partial dependency. But Laravel tables often have anidand alsounique(['order_id', 'product_id']), a rule that the pair can't repeat. That pair is a candidate key too, soproduct_namein that table is still a partial dependency. When every key really is one column, any problem left belongs to Third Normal Form (3NF), the next lesson. - Moving a column that depends fully. The price paid in that order (
price_paid) depends on the order and the product, because the price of Milk Coffee may go up next month. Soprice_paidstays inorder_items. - Forgetting columns that lean on the other key column.
order_datedepends onorder_idalone, so this column is a partial dependency too, and it belongs in theorderstable. - The order of the checks in the exercise. In the exercise, your function gets
pkColumns(the number of columns in the key) anddependentOn(the number of key columns it leans on). Check for a single-column key first (pkColumns <= 1), then comparedependentOnwithpkColumns. If you check in the other order,(1, 1)comes out as "full dependency".
TL;DR: 2NF (Second Normal Form, the second rule of normalisation) = already 1NF (each cell holds one value), and every non-key column (a column outside the primary key) depends on the whole primary key. Its violation is called a partial dependency: a column that depends on only part of the key. A partial dependency can only appear with a combined key, including a pair of columns marked unique beside an id. For example, product_name in order_items (order_id, product_id) depends on product_id alone. The result is an update anomaly (one product with two names), a delete anomaly (cancelling an order also deletes a product from the menu) and an insert anomaly (a new product can't be recorded before it is ordered). The fix: move that column to its own table (products) and link it by product_id. Every key a single column = no partial possible.
⚡ Depend on the whole key
Meaning: 2NF (Second Normal Form) is the second rule of normalisation: the table already meets 1NF, and every non-key column (a column outside the primary key) depends on the whole primary key. Column B depends on column A when, once you know A, there is exactly one possible value of B. In order_items with the combined primary key (order_id, product_id), qty (the quantity) needs both, but product_name needs only product_id: product 1 is always Milk Coffee. A column that depends on only part of the key, such as product_name, has a partial dependency.
⚡ One product, two names
Write:
UPDATE order_items SET product_name = 'Sunset Milk Coffee'
WHERE order_id = 1 AND product_id = 1;
Guess first: the name of Milk Coffee (product 1) is written in two order_items rows, in order 1 and order 3. This code changes only the row of order 1. After this, does Milk Coffee come out with one name or two?
Two. Order 1 now says Sunset Milk Coffee, order 3 still says Milk Coffee, and the database doesn't refuse it. This is called an update anomaly.
Meaning: A column with a partial dependency, such as product_name, is written again on many rows, so it easily contradicts itself.
⚡ Move it to its own table
Write:
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
Meaning: The product name moves to its own table, products, and is stored there once. order_items no longer keeps the name, only order_id, product_id and qty. The primary key of products is a single column (id), and a single-column key has no "part", so it can't have a partial dependency.
In the exercise, pkColumns is the number of columns in the key, and dependentOn is the number of key columns a column leans on (depends on). Check pkColumns <= 1 first, then compare dependentOn with pkColumns.