Second Normal Form (2NF) — Database

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…

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:

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);

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?

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.

What you will learn