Reducing conflicting facts

Lesson 14 of 18 · SQL foundations

Normalization organizes facts to reduce duplication and update anomalies. If every book row repeats a shelf label, renaming that shelf requires several updates; a missed row creates conflicting labels. Storing the label once in shelves and referring to its key avoids that particular anomaly.

A functional dependency means a value or combination determines another under the business rules, not merely by coincidence in a sample. A candidate key is a minimal set of columns that uniquely identifies a row; a table may have several. A composite key uses more than one column. A superkey uniquely identifies a row, possibly with extra attributes. A non-key (non-prime) attribute is one that belongs to no candidate key; a nontrivial dependency determines something not already included among its determining attributes.

  • 1NF: represent attributes as single values from their domains, rather than repeating groups; identify rows with keys.
  • 2NF: in 1NF, no non-key attribute depends on a proper subset of a candidate key—some, but not all, of its columns. For (book_id, topic_id), the book title belongs to the book, not the pair.
  • 3NF: for each nontrivial dependency X → A, X is a superkey or A is part of some candidate key. Informally, avoid non-key facts depending transitively on a key through another non-key fact.
  • BCNF: for each nontrivial dependency X → A, X must be a superkey. This stronger rule handles some overlapping candidate-key cases that 3NF allows.

These are design rules based on known dependencies, not a test of sample rows alone. Decompositions must preserve information; sometimes preserving every dependency requires a trade-off. Derived values such as age can become stale; compute them where appropriate. Denormalization can be justified by measured needs, but requires a consistency strategy.

Trace a decomposition

Suppose a single table records (book_id, topic_id, title, topic_name), with one row per book–topic pair. Assume titles and topic names need not be unique, so neither identifies a book or topic, and the pair is the only candidate key. Under the rules that each book has one title and each topic has one name, book_id alone determines title. That partial dependency violates 2NF. Moving titles to books and names to topics, while keeping the pairs in book_topics, lets us rename a topic once. Joining those tables on their keys recovers the associations; joins are explained in the next chapter. A book with no topics still needs its own books row; an inner join to the association table would omit it from that particular result.

For a 3NF example, imagine storing (book_id, shelf_id, shelf_label) with one shelf per book and the rule shelf_id → shelf_label. The label depends on the book ID through a non-key shelf ID. Store the label with the shelf instead. These examples assume the stated business rules; different rules can change the keys and dependencies. Formal normal-form analysis uses attributes participating in any candidate key, not just the chosen primary key. Lossless joins and preserved constraints must be checked for the actual decomposition.

Worked progression: 1NF to 3NF

Assume each book has one title and one shelf, each shelf ID has one label, and a book can have several topics. IDs, not names, identify books, topics and shelves. Start with a report that places multiple topics in one cell:

book_id title shelf_id shelf_label topics (id: name)
1 Moss Atlas 1 Nature 10: Plants, 20: Maps
2 River Notes 1 Nature 10: Plants

1NF: Treat each topic as a separate fact, with one topic per row. Under these rules (book_id, topic_id) is the only candidate key of the resulting association report. A comma-separated list is text to SQLite, but hiding several topic values in it makes individual associations difficult to constrain or query.

book_id topic_id title topic_name shelf_id shelf_label
1 10 Moss Atlas Plants 1 Nature
1 20 Moss Atlas Maps 1 Nature
2 10 River Notes Plants 1 Nature

2NF: book_id → title, shelf_id, shelf_label and topic_id → topic_name: each determinant is only part of the pair. Split into these tables; the pair table retains the associations. The books table still repeats Nature, which is the next problem.

books: book_id title shelf_id shelf_label
1 Moss Atlas 1 Nature
2 River Notes 1 Nature
topics: topic_id topic_name
10 Plants
20 Maps
book_topics: book_id topic_id
1 10
1 20
2 10

3NF: In books, book_id → shelf_id → shelf_label, and shelf_id is not a candidate key. Move the label to a shelf table. Now a shelf rename changes one row; the shelf can exist even when no book uses it.

shelves: shelf_id shelf_label
1 Nature
books: book_id title shelf_id
1 Moss Atlas 1
2 River Notes 1

The following self-contained SQLite example creates the final design in a new empty database. It enforces the keys and references shown, not every possible business rule. Foreign keys must be enabled on the connection outside a transaction. Run it independently of the course’s library fixture, which uses some of the same table names.

PRAGMA foreign_keys = ON;
CREATE TABLE shelves (shelf_id INTEGER PRIMARY KEY, shelf_label TEXT NOT NULL);
CREATE TABLE books (book_id INTEGER PRIMARY KEY, title TEXT NOT NULL,
                    shelf_id INTEGER NOT NULL REFERENCES shelves(shelf_id));
CREATE TABLE topics (topic_id INTEGER PRIMARY KEY, topic_name TEXT NOT NULL);
CREATE TABLE book_topics (book_id INTEGER NOT NULL REFERENCES books(book_id),
                          topic_id INTEGER NOT NULL REFERENCES topics(topic_id),
                          PRIMARY KEY (book_id, topic_id));
INSERT INTO shelves VALUES (1, 'Nature');
INSERT INTO books VALUES (1, 'Moss Atlas', 1), (2, 'River Notes', 1);
INSERT INTO topics VALUES (10, 'Plants'), (20, 'Maps');
INSERT INTO book_topics VALUES (1, 10), (1, 20), (2, 10);
SELECT books.book_id, topics.topic_id, books.title, topics.topic_name,
       shelves.shelf_id, shelves.shelf_label
FROM books JOIN book_topics ON books.book_id = book_topics.book_id
JOIN topics ON topics.topic_id = book_topics.topic_id
JOIN shelves ON shelves.shelf_id = books.shelf_id
ORDER BY books.book_id, topics.topic_id;

The three result rows reproduce the 1NF table above, in the same column order. For these rules, each split joins back on the determinant, so it does not invent associations; the inner join recovers the original association rows, not books without topics. A different set of dependencies needs its own lossless-join and constraint analysis. SQLite does not infer these dependencies from the sample data.

Chapter index · Course index