Database Normalization · Lesson 36 of 112

Database Second Normal Form

What you will learn

  • The rule of second normal form (2NF)
  • What a partial dependency is
  • How a partial dependency causes an update anomaly
  • How to split the table to fix it

1. The rule

A table is in 2NF when it is in 1NF and every non-prime column (a column that is not part of any candidate key, lesson 5.2) depends on the whole of every candidate key, not just part of it.

2NF only matters when a candidate key has more than one column (a composite key). If every candidate key is a single column, the table is automatically in 2NF once it is in 1NF.

2. A partial dependency

An order can hold several products, so order lines are identified by the pair (order_id, product_id):

order_id product_id product_name qty
1 100 Pen 3
1 200 Book 1
2 100 Pen 10
3 200 Book 2

The dependencies:

  • (order_id, product_id) -> qty: the quantity needs both. Good.
  • product_id -> product_name: the name needs only part of the key. This is a partial dependency, and it breaks 2NF.

The symptom is the repeated Pen and Book. 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. Now the update anomaly:

CREATE TABLE lines_bad (
    order_id     INTEGER,
    product_id   INTEGER,
    product_name TEXT,
    qty          INTEGER,
    PRIMARY KEY (order_id, product_id)
);
INSERT INTO lines_bad VALUES (1, 100, 'Pen', 3), (1, 200, 'Book', 1), (2, 100, 'Pen', 10), (3, 200, 'Book', 2);
(4 rows changed)

Rename the pen, but only on order 1's line:

UPDATE lines_bad SET product_name = 'Ball pen' WHERE order_id = 1 AND product_id = 100;
SELECT DISTINCT product_id, product_name FROM lines_bad ORDER BY product_id, product_name;
product_id  product_name
----------  ------------
       100  Ball pen
       100  Pen
       200  Book
(3 rows)
CREATE TABLE lines_bad (
    order_id     INTEGER,
    product_id   INTEGER,
    product_name TEXT,
    qty          INTEGER,
    PRIMARY KEY (order_id, product_id)
);
INSERT INTO lines_bad VALUES (1, 100, 'Pen', 3), (1, 200, 'Book', 1), (2, 100, 'Pen', 10), (3, 200, 'Book', 2);
(4 rows changed)

Rename the pen, but only on order 1's line:

UPDATE lines_bad SET product_name = 'Ball pen' WHERE order_id = 1 AND product_id = 100;
SELECT DISTINCT product_id, product_name FROM lines_bad ORDER BY product_id, product_name;
product_id  product_name
----------  ------------
       100  Ball pen
       100  Pen
       200  Book
(3 rows)
CREATE TABLE lines_bad (
    order_id     INTEGER,
    product_id   INTEGER,
    product_name TEXT,
    qty          INTEGER,
    PRIMARY KEY (order_id, product_id)
);
INSERT INTO lines_bad VALUES (1, 100, 'Pen', 3), (1, 200, 'Book', 1), (2, 100, 'Pen', 10), (3, 200, 'Book', 2);
(4 rows changed)

Rename the pen, but only on order 1's line:

UPDATE lines_bad SET product_name = 'Ball pen' WHERE order_id = 1 AND product_id = 100;
SELECT DISTINCT product_id, product_name FROM lines_bad ORDER BY product_id, product_name;
product_id  product_name
----------  ------------
       100  Ball pen
       100  Pen
       200  Book
(3 rows)

Product 100 now has two names. There is a deletion problem too: if every order line for a product is deleted, the product's name is lost.

3. The fix

Move the partly-dependent column into a table whose key is that part:

product_id product_name
100 Pen
200 Book
300 Bag
order_id product_id qty
1 100 3
1 200 1
2 100 10
3 200 2
CREATE TABLE catalog (product_id INTEGER PRIMARY KEY, product_name TEXT);
CREATE TABLE lines_good (
    order_id   INTEGER,
    product_id INTEGER REFERENCES catalog (product_id),
    qty        INTEGER,
    PRIMARY KEY (order_id, product_id)
);
INSERT INTO catalog VALUES (100, 'Pen'), (200, 'Book'), (300, 'Bag');
INSERT INTO lines_good VALUES (1, 100, 3), (1, 200, 1), (2, 100, 10), (3, 200, 2);
UPDATE catalog SET product_name = 'Ball pen' WHERE product_id = 100;
(1 row changed)
SELECT i.order_id, p.product_name, i.qty
FROM lines_good i JOIN catalog p USING (product_id)
ORDER BY i.order_id, p.product_id;
order_id  product_name  qty
--------  ------------  ---
       1  Ball pen        3
       1  Book            1
       2  Ball pen       10
       3  Book            2
(4 rows)
SELECT product_name FROM catalog WHERE product_id = 300;
product_name
------------
Bag
(1 row)
CREATE TABLE catalog (product_id INTEGER PRIMARY KEY, product_name TEXT);
CREATE TABLE lines_good (
    order_id   INTEGER,
    product_id INTEGER REFERENCES catalog (product_id),
    qty        INTEGER,
    PRIMARY KEY (order_id, product_id)
);
INSERT INTO catalog VALUES (100, 'Pen'), (200, 'Book'), (300, 'Bag');
INSERT INTO lines_good VALUES (1, 100, 3), (1, 200, 1), (2, 100, 10), (3, 200, 2);
UPDATE catalog SET product_name = 'Ball pen' WHERE product_id = 100;
(1 row changed)
SELECT i.order_id, p.product_name, i.qty
FROM lines_good i JOIN catalog p USING (product_id)
ORDER BY i.order_id, p.product_id;
order_id  product_name  qty
--------  ------------  ---
       1  Ball pen        3
       1  Book            1
       2  Ball pen       10
       3  Book            2
(4 rows)
SELECT product_name FROM catalog WHERE product_id = 300;
product_name
------------
Bag
(1 row)
CREATE TABLE catalog (product_id INTEGER PRIMARY KEY, product_name TEXT);
CREATE TABLE lines_good (
    order_id   INTEGER,
    product_id INTEGER REFERENCES catalog (product_id),
    qty        INTEGER,
    PRIMARY KEY (order_id, product_id)
);
INSERT INTO catalog VALUES (100, 'Pen'), (200, 'Book'), (300, 'Bag');
INSERT INTO lines_good VALUES (1, 100, 3), (1, 200, 1), (2, 100, 10), (3, 200, 2);
UPDATE catalog SET product_name = 'Ball pen' WHERE product_id = 100;
(1 row changed)
SELECT i.order_id, p.product_name, i.qty
FROM lines_good i JOIN catalog p USING (product_id)
ORDER BY i.order_id, p.product_id;
order_id  product_name  qty
--------  ------------  ---
       1  Ball pen        3
       1  Book            1
       2  Ball pen       10
       3  Book            2
(4 rows)
SELECT product_name FROM catalog WHERE product_id = 300;
product_name
------------
Bag
(1 row)

One update renamed the pen everywhere, and the Bag (never ordered) is still stored. In the new order lines table, qty depends on the whole key (order_id, product_id).

The sample databases have composite keys that are already in 2NF. Each non-key column depends on the whole key:

order_items has the key (order_id, line_no). The product name is not stored there, only product_id; the name lives in products.

SELECT name, type FROM pragma_table_info('order_items') ORDER BY cid;
name        type
----------  -------
order_id    INTEGER
line_no     INTEGER
product_id  INTEGER
quantity    INTEGER
unit_price  REAL
(5 rows)

loan_payments has its own key payment_id. The loan's customer is not copied into every payment, only loan_id is.

SELECT name, type FROM pragma_table_info('loan_payments') ORDER BY cid;
name          type
------------  -------
payment_id    INTEGER
loan_id       INTEGER
paid_on       TEXT
amount_cents  INTEGER
(4 rows)

enrollments has the key (student_id, section_id). The student's name and the course title are not stored there, only the two ids, the date and the grade (which really depends on both).

SELECT name, type FROM pragma_table_info('enrollments') ORDER BY cid;
name         type
-----------  -------
student_id   INTEGER
section_id   INTEGER
enrolled_on  TEXT
grade        REAL
(4 rows)

Worked example: find the partial dependency

Table enrolments (student_id, course_id, student_name, course_title, grade) with key (student_id, course_id).

Column Depends on Verdict
grade the student and the course whole key: fine
student_name student_id only partial: move out
course_title course_id only partial: move out

Result: students (student_id, student_name), courses (course_id, course_title), enrolments (student_id, course_id, grade). Names are made up.

Common mistakes

  • Checking 2NF on a single-column key. There is no "part of the key" to depend on.
  • Putting the price into order lines without thinking. If the price at the time of sale must be kept, it depends on the whole line and belongs there; see lesson 5.7.
  • Splitting but dropping the foreign key, so the pieces cannot be joined safely.

Key takeaways

  • 2NF = 1NF + no column depends on only part of a composite key.
  • A partial dependency shows up as a repeated value tied to part of the key.
  • Fix: move the column to a table keyed by that part of the key.
  • A candidate key of one column cannot have partial dependencies.

Practice

1. When can a table violate 2NF?

Show answer

Only when its key has more than one column.

2. In (order_id, product_id, product_name, qty), which column breaks 2NF?

Show answer

product_name, because it depends only on product_id.

3. What went wrong when only one order line was renamed?

Show answer

Product 100 had two names, Ball pen and Pen: an update anomaly.

4. Split (student_id, course_id, student_name, grade), key (student_id, course_id).

Show answer

students (student_id, student_name) and enrolments (student_id, course_id, grade).

5. Why did the Bag survive in the new design?

Show answer

Products now have their own table, so a product exists without any order line.

6. Is a 1NF table whose only candidate key is emp_id automatically 2NF?

Show answer

Yes: with a single-column candidate key, no column can depend on part of it. (If the table also had a composite candidate key, you would have to check that one as well.)