Database Normalization · Lesson 38 of 112

Database BCNF and Beyond

What you will learn

  • The rule of Boyce-Codd normal form (BCNF)
  • A table that is in 3NF but not BCNF
  • The trade-off of splitting: a lost dependency
  • What 4NF and 5NF are about, briefly

1. The rule

A table is in BCNF when, for every functional dependency X -> Y in it, X is a key (or contains a key). It is a slightly stricter form of 3NF. 3NF lets an exception through when the right side is part of a key; BCNF does not.

Many tables in 3NF are already in BCNF. The difference appears only when a table has several overlapping candidate keys (lesson 2.6), so that a column can be part of a key and still be determined by a non-key.

2. A table that is 3NF but not BCNF

A school rule: each teacher teaches one subject, a subject can have several teachers, and a student has one teacher per subject.

student subject teacher
Rina Math Mr Ali
Rina Art Ms Bela
Sam Math Mr Ali
Lee Math Mr Ali
Lee Art Ms Cara

The dependencies:

  • (student, subject) -> teacher: a student has one teacher per subject. (student, subject) is a candidate key.
  • teacher -> subject: each teacher teaches one subject. teacher is not a key.

The two FDs together make (student, teacher) a second candidate key: it determines subject (through teacher -> subject) and itself includes teacher. The two keys overlap on student, and subject and teacher are each prime. That is why 3NF accepts the table: the right side of teacher -> subject is a prime column. BCNF does not accept it, because teacher is not a key. The problem is still repeated data: "Mr Ali teaches Math" is stored in every row for every student of his.

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.

CREATE TABLE lessons (student TEXT, subject TEXT, teacher TEXT, PRIMARY KEY (student, subject));
INSERT INTO lessons VALUES
    ('Rina', 'Math', 'Mr Ali'), ('Rina', 'Art', 'Ms Bela'), ('Sam', 'Math', 'Mr Ali'),
    ('Lee', 'Math', 'Mr Ali'), ('Lee', 'Art', 'Ms Cara');
SELECT COUNT(*) AS teachers_with_more_than_one_subject
FROM (SELECT teacher FROM lessons GROUP BY teacher HAVING COUNT(DISTINCT subject) > 1);
teachers_with_more_than_one_subject
-----------------------------------
                                  0
(1 row)
CREATE TABLE lessons (student TEXT, subject TEXT, teacher TEXT, PRIMARY KEY (student, subject));
INSERT INTO lessons VALUES
    ('Rina', 'Math', 'Mr Ali'), ('Rina', 'Art', 'Ms Bela'), ('Sam', 'Math', 'Mr Ali'),
    ('Lee', 'Math', 'Mr Ali'), ('Lee', 'Art', 'Ms Cara');
SELECT COUNT(*) AS teachers_with_more_than_one_subject
FROM (SELECT teacher FROM lessons GROUP BY teacher HAVING COUNT(DISTINCT subject) > 1);
teachers_with_more_than_one_subject
-----------------------------------
                                  0
(1 row)
CREATE TABLE lessons (student TEXT, subject TEXT, teacher TEXT, PRIMARY KEY (student, subject));
INSERT INTO lessons VALUES
    ('Rina', 'Math', 'Mr Ali'), ('Rina', 'Art', 'Ms Bela'), ('Sam', 'Math', 'Mr Ali'),
    ('Lee', 'Math', 'Mr Ali'), ('Lee', 'Art', 'Ms Cara');
SELECT COUNT(*) AS teachers_with_more_than_one_subject
FROM (SELECT teacher FROM lessons GROUP BY teacher HAVING COUNT(DISTINCT subject) > 1);
teachers_with_more_than_one_subject
-----------------------------------
                                  0
(1 row)

(The data agrees with teacher -> subject; the rule itself comes from the school.)

3. The BCNF split

Put each dependency into its own table: teacher -> subject becomes a teacher_subjects table, and the students link to teachers.

CREATE TABLE teacher_subjects (teacher TEXT PRIMARY KEY, subject TEXT NOT NULL);
CREATE TABLE class_enrolments (
    student TEXT,
    teacher TEXT REFERENCES teacher_subjects (teacher),
    PRIMARY KEY (student, teacher)
);
INSERT INTO teacher_subjects VALUES ('Mr Ali', 'Math'), ('Ms Bela', 'Art'), ('Ms Cara', 'Art');
INSERT INTO class_enrolments VALUES ('Rina', 'Mr Ali'), ('Rina', 'Ms Bela'), ('Sam', 'Mr Ali'), ('Lee', 'Mr Ali'), ('Lee', 'Ms Cara');
SELECT e.student, t.subject, e.teacher
FROM class_enrolments e JOIN teacher_subjects t USING (teacher)
ORDER BY e.student, t.subject;
student  subject  teacher
-------  -------  -------
Lee      Art      Ms Cara
Lee      Math     Mr Ali
Rina     Art      Ms Bela
Rina     Math     Mr Ali
Sam      Math     Mr Ali
(5 rows)
CREATE TABLE teacher_subjects (teacher TEXT PRIMARY KEY, subject TEXT NOT NULL);
CREATE TABLE class_enrolments (
    student TEXT,
    teacher TEXT REFERENCES teacher_subjects (teacher),
    PRIMARY KEY (student, teacher)
);
INSERT INTO teacher_subjects VALUES ('Mr Ali', 'Math'), ('Ms Bela', 'Art'), ('Ms Cara', 'Art');
INSERT INTO class_enrolments VALUES ('Rina', 'Mr Ali'), ('Rina', 'Ms Bela'), ('Sam', 'Mr Ali'), ('Lee', 'Mr Ali'), ('Lee', 'Ms Cara');
SELECT e.student, t.subject, e.teacher
FROM class_enrolments e JOIN teacher_subjects t USING (teacher)
ORDER BY e.student, t.subject;
student  subject  teacher
-------  -------  -------
Lee      Art      Ms Cara
Lee      Math     Mr Ali
Rina     Art      Ms Bela
Rina     Math     Mr Ali
Sam      Math     Mr Ali
(5 rows)
CREATE TABLE teacher_subjects (teacher TEXT PRIMARY KEY, subject TEXT NOT NULL);
CREATE TABLE class_enrolments (
    student TEXT,
    teacher TEXT REFERENCES teacher_subjects (teacher),
    PRIMARY KEY (student, teacher)
);
INSERT INTO teacher_subjects VALUES ('Mr Ali', 'Math'), ('Ms Bela', 'Art'), ('Ms Cara', 'Art');
INSERT INTO class_enrolments VALUES ('Rina', 'Mr Ali'), ('Rina', 'Ms Bela'), ('Sam', 'Mr Ali'), ('Lee', 'Mr Ali'), ('Lee', 'Ms Cara');
SELECT e.student, t.subject, e.teacher
FROM class_enrolments e JOIN teacher_subjects t USING (teacher)
ORDER BY e.student, t.subject;
student  subject  teacher
-------  -------  -------
Lee      Art      Ms Cara
Lee      Math     Mr Ali
Rina     Art      Ms Bela
Rina     Math     Mr Ali
Sam      Math     Mr Ali
(5 rows)

The join rebuilds the original rows, and "Mr Ali teaches Math" is stored once.

4. The price: a lost dependency

The original rule "a student has one teacher per subject" ((student, subject) -> teacher) was enforced by the old primary key. After the split, no single table holds it, so the database no longer enforces it. Rina now gets a second Art teacher:

INSERT INTO class_enrolments VALUES ('Rina', 'Ms Cara');
SELECT e.student, t.subject, COUNT(*) AS teachers
FROM class_enrolments e JOIN teacher_subjects t USING (teacher)
GROUP BY e.student, t.subject
HAVING COUNT(*) > 1;
student  subject  teachers
-------  -------  --------
Rina     Art             2
(1 row)
INSERT INTO class_enrolments VALUES ('Rina', 'Ms Cara');
SELECT e.student, t.subject, COUNT(*) AS teachers
FROM class_enrolments e JOIN teacher_subjects t USING (teacher)
GROUP BY e.student, t.subject
HAVING COUNT(*) > 1;
student  subject  teachers
-------  -------  --------
Rina     Art             2
(1 row)
INSERT INTO class_enrolments VALUES ('Rina', 'Ms Cara');
SELECT e.student, t.subject, COUNT(*) AS teachers
FROM class_enrolments e JOIN teacher_subjects t USING (teacher)
GROUP BY e.student, t.subject
HAVING COUNT(*) > 1;
student  subject  teachers
-------  -------  --------
Rina     Art             2
(1 row)

The insert was accepted, and Rina has two Art teachers, breaking a school rule. This is a known limit: BCNF decomposition cannot always keep every dependency enforceable. You then choose between (a) staying in 3NF with a little redundancy and keeping the rule enforced by the key, or (b) BCNF and enforcing the lost rule in application code or with a trigger. Both are legitimate engineering choices.

5. Beyond BCNF: 4NF and 5NF

Fourth normal form (4NF) deals with independent many-to-many facts in one table. If an employee has several skills and several languages, and the two are unrelated, a single table must store every combination:

CREATE TABLE people_flat (name TEXT, skill TEXT, language TEXT, PRIMARY KEY (name, skill, language));
INSERT INTO people_flat VALUES
    ('Rina', 'SQL', 'Bangla'), ('Rina', 'SQL', 'English'),
    ('Rina', 'Python', 'Bangla'), ('Rina', 'Python', 'English');
INSERT INTO people_flat VALUES ('Rina', 'Excel', 'Bangla');
SELECT COUNT(*) AS rows_stored FROM people_flat;
rows_stored
-----------
          5
(1 row)
CREATE TABLE people_flat (name TEXT, skill TEXT, language TEXT, PRIMARY KEY (name, skill, language));
INSERT INTO people_flat VALUES
    ('Rina', 'SQL', 'Bangla'), ('Rina', 'SQL', 'English'),
    ('Rina', 'Python', 'Bangla'), ('Rina', 'Python', 'English');
INSERT INTO people_flat VALUES ('Rina', 'Excel', 'Bangla');
SELECT COUNT(*) AS rows_stored FROM people_flat;
rows_stored
-----------
          5
(1 row)
CREATE TABLE people_flat (name TEXT, skill TEXT, language TEXT, PRIMARY KEY (name, skill, language));
INSERT INTO people_flat VALUES
    ('Rina', 'SQL', 'Bangla'), ('Rina', 'SQL', 'English'),
    ('Rina', 'Python', 'Bangla'), ('Rina', 'Python', 'English');
INSERT INTO people_flat VALUES ('Rina', 'Excel', 'Bangla');
SELECT COUNT(*) AS rows_stored FROM people_flat;
rows_stored
-----------
          5
(1 row)

Adding the skill Excel required rows for every language she speaks. The code added only the Bangla row, so the table now implies she cannot use Excel in English. The fix is two tables, one per independent fact:

CREATE TABLE skills (name TEXT, skill TEXT, PRIMARY KEY (name, skill));
CREATE TABLE languages (name TEXT, language TEXT, PRIMARY KEY (name, language));
INSERT INTO skills VALUES ('Rina', 'SQL'), ('Rina', 'Python'), ('Rina', 'Excel');
INSERT INTO languages VALUES ('Rina', 'Bangla'), ('Rina', 'English');
SELECT (SELECT COUNT(*) FROM skills) AS skill_rows, (SELECT COUNT(*) FROM languages) AS language_rows;
skill_rows  language_rows
----------  -------------
         3              2
(1 row)
CREATE TABLE skills (name TEXT, skill TEXT, PRIMARY KEY (name, skill));
CREATE TABLE languages (name TEXT, language TEXT, PRIMARY KEY (name, language));
INSERT INTO skills VALUES ('Rina', 'SQL'), ('Rina', 'Python'), ('Rina', 'Excel');
INSERT INTO languages VALUES ('Rina', 'Bangla'), ('Rina', 'English');
SELECT (SELECT COUNT(*) FROM skills) AS skill_rows, (SELECT COUNT(*) FROM languages) AS language_rows;
skill_rows  language_rows
----------  -------------
         3              2
(1 row)
CREATE TABLE skills (name TEXT, skill TEXT, PRIMARY KEY (name, skill));
CREATE TABLE languages (name TEXT, language TEXT, PRIMARY KEY (name, language));
INSERT INTO skills VALUES ('Rina', 'SQL'), ('Rina', 'Python'), ('Rina', 'Excel');
INSERT INTO languages VALUES ('Rina', 'Bangla'), ('Rina', 'English');
SELECT (SELECT COUNT(*) FROM skills) AS skill_rows, (SELECT COUNT(*) FROM languages) AS language_rows;
skill_rows  language_rows
----------  -------------
         3              2
(1 row)

Three skills and two languages take 3 + 2 = 5 rows instead of 3 x 2 = 6 combinations, and the gap grows with every addition.

Formally, 4NF is about multivalued dependencies: name ->> skill means that the set of skills of a person does not depend on their languages. A table is in 4NF when every such dependency is implied by a key.

Fifth normal form (5NF) deals with a table that can be rebuilt exactly by joining three (or more) smaller tables but not by joining just two of them (a join dependency). Suppose the rule is: if supplier S supplies part P, project J uses part P, and S works on project J, then S supplies P to J. Then the three-column table can be stored as three two-column tables:

CREATE TABLE spj (supplier TEXT, part TEXT, project TEXT, PRIMARY KEY (supplier, part, project));
INSERT INTO spj VALUES ('Acme', 'bolt', 'Bridge'), ('Acme', 'bolt', 'Tower'), ('Acme', 'nut', 'Bridge'), ('Best', 'bolt', 'Tower');
CREATE TABLE sp AS SELECT DISTINCT supplier, part FROM spj;
CREATE TABLE pj AS SELECT DISTINCT part, project FROM spj;
CREATE TABLE sj AS SELECT DISTINCT supplier, project FROM spj;
(4 rows changed)

Rebuild the original by joining the three small tables. Count the rows that are missing and the rows that are extra:

SELECT
  (SELECT COUNT(*) FROM (SELECT sp.supplier, sp.part, pj.project FROM sp JOIN pj USING (part)
        JOIN sj ON sj.supplier = sp.supplier AND sj.project = pj.project
        EXCEPT SELECT * FROM spj)) AS extra_rows,
  (SELECT COUNT(*) FROM (SELECT * FROM spj
        EXCEPT SELECT sp.supplier, sp.part, pj.project FROM sp JOIN pj USING (part)
        JOIN sj ON sj.supplier = sp.supplier AND sj.project = pj.project)) AS missing_rows;
extra_rows  missing_rows
----------  ------------
         0             0
(1 row)

Now add the single fact "Best works on Bridge" and list what the join implies:

INSERT INTO sj VALUES ('Best', 'Bridge');
SELECT sp.supplier, sp.part, pj.project
FROM sp JOIN pj USING (part)
JOIN sj ON sj.supplier = sp.supplier AND sj.project = pj.project
EXCEPT SELECT * FROM spj;
supplier  part  project
--------  ----  -------
Best      bolt  Bridge
(1 row)
CREATE TABLE spj (supplier TEXT, part TEXT, project TEXT, PRIMARY KEY (supplier, part, project));
INSERT INTO spj VALUES ('Acme', 'bolt', 'Bridge'), ('Acme', 'bolt', 'Tower'), ('Acme', 'nut', 'Bridge'), ('Best', 'bolt', 'Tower');
CREATE TABLE sp AS SELECT DISTINCT supplier, part FROM spj;
CREATE TABLE pj AS SELECT DISTINCT part, project FROM spj;
CREATE TABLE sj AS SELECT DISTINCT supplier, project FROM spj;
(4 rows changed)

Rebuild the original by joining the three small tables. Count the rows that are missing and the rows that are extra:

SELECT
  (SELECT COUNT(*) FROM (SELECT sp.supplier, sp.part, pj.project FROM sp JOIN pj USING (part)
        JOIN sj ON sj.supplier = sp.supplier AND sj.project = pj.project
        EXCEPT SELECT * FROM spj)) AS extra_rows,
  (SELECT COUNT(*) FROM (SELECT * FROM spj
        EXCEPT SELECT sp.supplier, sp.part, pj.project FROM sp JOIN pj USING (part)
        JOIN sj ON sj.supplier = sp.supplier AND sj.project = pj.project)) AS missing_rows;
extra_rows  missing_rows
----------  ------------
         0             0
(1 row)

Now add the single fact "Best works on Bridge" and list what the join implies:

INSERT INTO sj VALUES ('Best', 'Bridge');
SELECT sp.supplier, sp.part, pj.project
FROM sp JOIN pj USING (part)
JOIN sj ON sj.supplier = sp.supplier AND sj.project = pj.project
EXCEPT SELECT * FROM spj;
supplier  part  project
--------  ----  -------
Best      bolt  Bridge
(1 row)
CREATE TABLE spj (supplier TEXT, part TEXT, project TEXT, PRIMARY KEY (supplier, part, project));
INSERT INTO spj VALUES ('Acme', 'bolt', 'Bridge'), ('Acme', 'bolt', 'Tower'), ('Acme', 'nut', 'Bridge'), ('Best', 'bolt', 'Tower');
CREATE TABLE sp AS SELECT DISTINCT supplier, part FROM spj;
CREATE TABLE pj AS SELECT DISTINCT part, project FROM spj;
CREATE TABLE sj AS SELECT DISTINCT supplier, project FROM spj;
(4 rows changed)

Rebuild the original by joining the three small tables. Count the rows that are missing and the rows that are extra:

SELECT
  (SELECT COUNT(*) FROM (SELECT sp.supplier, sp.part, pj.project FROM sp JOIN pj USING (part)
        JOIN sj ON sj.supplier = sp.supplier AND sj.project = pj.project
        EXCEPT SELECT * FROM spj)) AS extra_rows,
  (SELECT COUNT(*) FROM (SELECT * FROM spj
        EXCEPT SELECT sp.supplier, sp.part, pj.project FROM sp JOIN pj USING (part)
        JOIN sj ON sj.supplier = sp.supplier AND sj.project = pj.project)) AS missing_rows;
extra_rows  missing_rows
----------  ------------
         0             0
(1 row)

Now add the single fact "Best works on Bridge" and list what the join implies:

INSERT INTO sj VALUES ('Best', 'Bridge');
SELECT sp.supplier, sp.part, pj.project
FROM sp JOIN pj USING (part)
JOIN sj ON sj.supplier = sp.supplier AND sj.project = pj.project
EXCEPT SELECT * FROM spj;
supplier  part  project
--------  ----  -------
Best      bolt  Bridge
(1 row)

The first result shows that the join of the three small tables gives back exactly the original rows (nothing missing, nothing extra). In the three-table design, adding the single fact "Best works on Bridge" automatically makes the combination (Best, bolt, Bridge) true, as the rule says. In the single table someone would have to remember to add that row. 5NF situations are rare, and they depend on a business rule like this one; you will seldom need to apply it by hand.

Worked example: which normal form is violated?

Table Problem Form
phones column holds "a, b" list in a cell 1NF
(order_id, product_id, product_name, qty) product_name depends on part of key 2NF
(emp_id, dept_id, dept_name) dept_name depends on dept_id 3NF
(student, subject, teacher) with teacher -> subject determinant is not a key BCNF
(name, skill, language) with independent skills and languages two independent lists 4NF

Common mistakes

  • Chasing BCNF/4NF/5NF everywhere. For most real tables, 3NF is the practical target.
  • Ignoring a lost dependency. After a BCNF split, check which rules the database no longer enforces.
  • Reading BCNF as "more tables is always better". Each split must remove a real redundancy.

Key takeaways

  • BCNF: for every dependency X -> Y, X must be a key.
  • 3NF and BCNF differ only for tables with overlapping candidate keys.
  • A BCNF split may lose the ability to enforce a rule with a key.
  • 4NF separates independent many-to-many facts; 5NF is rare.
  • 3NF is the usual practical goal.

Practice

1. State the BCNF rule in one sentence.

Show answer

For every functional dependency X -> Y in the table, X must be a key.

2. What are the candidate keys of the lessons table, and which dependency violated BCNF?

Show answer

(student, subject) and (student, teacher). The dependency teacher -> subject violates BCNF, because teacher is not a key.

3. What rule did the BCNF split stop enforcing?

Show answer

That a student has only one teacher per subject. After the split Rina could have two Art teachers.

4. Why did adding Excel in the staff table cause trouble?

Show answer

Skills and languages are independent, so each new skill needs a row per language; adding only one row made the data look as if Excel could not be used in English.

5. How many rows are needed for 4 skills and 3 languages in one table, and in two tables?

Show answer

12 (4 x 3) in one table; 7 (4 + 3) in two.

6. For most applications, which normal form is the practical target?

Show answer

Third normal form (3NF), with BCNF when it costs nothing, and 4NF when independent lists appear.