SEN2104 · Week 10 · Relational Database Design

Canonical cover and normal forms: 1NF, 2NF, 3NF, BCNF

You learn to drop extraneous attributes to find the canonical cover, and to determine step by step which normal form a schema is in. The recipe for the 'What is the highest normal form?' question is on this page.

canonical coverextraneous attributefirst normal form (1NF)atomic domainsecond normal form (2NF)partial dependencythird normal form (3NF)transitive dependencyprime attributeBoyce-Codd normal form (BCNF)

Last week you saw closure and finding keys. This week you use them to do two things: reduce an FD set to its simplest form (canonical cover) and find the highest normal form of a schema. The question “What is the highest normal form of R?” is almost guaranteed in the exam. The recipe is always the same:

  1. Find all candidate keys.
  2. Write the prime attributes: those that appear in at least one candidate key.
  3. Test every non-trivial FD in turn against BCNF, 3NF and 2NF.

Canonical cover

There can be many FD sets that carry the same information. The canonical cover FcF_c is a set that is equivalent to FF (the same F+F^+) but contains nothing superfluous:

  • No FD has an extraneous attribute.
  • Every left side is unique (use A→BCA \to BC instead of A→BA \to B and A→CA \to C).

The extraneous attribute test

WhereQuestionTest
Left side: B in AB→CAB \to CDoes A alone determine C?Compute {A}+\{A\}^+ under the original F. Is C in it?
Right side: C in A→BCA \to BCCan I derive C without this FD?Change the FD to A→BA \to B. Does {A}+\{A\}^+ under the new set contain C?

PITFALL · under which F? In the left-side test, the closure is taken with the unchanged F. In the right-side test, you put the shortened FD into the set and take the closure. Mixing the two leads to wrong “extraneous” decisions.

Worked example

F={A→BD, AB→C, C→D, B→D}F = \{A \to BD,\ AB \to C,\ C \to D,\ B \to D\}

1) Split the right sides:   A -> B, A -> D, AB -> C, C -> D, B -> D

2) Left sides:              is B extraneous in AB -> C?
                            {A}+ (under F): A -> B, A -> D, AB -> C  →  ABCD   C is there → B is extraneous
                            is A extraneous? {B}+ = BD, no C → it is not
                            FD:  A -> C

3) Redundant FDs:           if A -> D is removed: {A}+ = A -> B, A -> C, C -> D → ABCD   D is there → remove
                            if B -> D is removed: {B}+ = B, no D → needed
                            if C -> D is removed: {C}+ = C, no D → needed
                            if A -> B is removed: {A}+ = ACD, no B → needed
                            if A -> C is removed: {A}+ = ABD, no C → needed

4) Combine:                 Fc = { A -> BC,  B -> D,  C -> D }

Final check: is FcF_c equivalent to FF? Every FD in FF can be derived from FcF_c, and the reverse holds too. Equivalent.

TIP · the canonical cover is not unique. If you do the redundancy tests in a different order, you may find a different but equivalent FcF_c. If you write your steps in the exam, both answers are accepted. The examples on this page were solved in order from left to right.

First normal form (1NF)

A schema is in 1NF if the domain of every attribute is atomic: no list, set or repeating group in a cell.

stu_idnamephones
S1Ada0532 111 22 33, 0216 444 55 66

This table is not in 1NF. Fix: a separate table student_phone(stu_id, phone), with each phone in its own row. In the relational model every table is assumed to be in 1NF by default; the following forms look at FDs.

Second normal form (2NF)

1NF + no non-prime attribute depends on a proper subset of a candidate key. This forbidden situation is called a partial dependency.

University example: takes_wide(stu_id, course_id, grade, stu_name, title), with FDs {stu_id, course_id}→grade\{\text{stu\_id, course\_id}\} \to \text{grade}, stu_id→stu_name\text{stu\_id} \to \text{stu\_name}, course_id→title\text{course\_id} \to \text{title}.

The only key is {stu_id, course_id}. stu_name depends only on stu_id, and title only on course_id. Both are partial dependencies: highest 1NF. The update anomaly of last week’s wide table came from exactly this.

Example with letters: R(A,B,C,D)R(A,B,C,D), F={AB→C, B→D}F = \{AB \to C,\ B \to D\}. The key is AB, and B→DB \to D is partial: 1NF.

PITFALL · a single-attribute key. If all candidate keys are single attributes, a partial dependency is impossible; if the schema is in 1NF, it is automatically in 2NF. In that case skip the 2NF test and go straight to 3NF.

Third normal form (3NF)

For every non-trivial X→AX \to A, at least one of the following must be true:

  1. XX is a superkey, or
  2. AA is a prime attribute.

The typical violating case is a transitive dependency: key → non-key → non-key.

University example: student_dept(stu_id, name, dept_name, building), with FDs stu_id→name, dept_name\text{stu\_id} \to \text{name, dept\_name}, dept_name→building\text{dept\_name} \to \text{building}. The key is stu_id (a single attribute, so 2NF is automatic). dept_name→building\text{dept\_name} \to \text{building}: dept_name is not a superkey, and building is not prime. 3NF is violated: highest 2NF.

Example with letters: R(A,B,C,D)R(A,B,C,D), F={A→B, B→C, A→D}F = \{A \to B,\ B \to C,\ A \to D\}: the key is A, B→CB \to C is transitive, 2NF.

Boyce-Codd normal form (BCNF)

For every non-trivial X→YX \to Y, XX must be a superkey. There is no “or A is prime” escape route as in 3NF. Every BCNF schema is in 3NF; the reverse is not always true.

The classic case that is in 3NF but not in BCNF: a non-key attribute determines part of a key. University example: tutoring(stu_id, subject, tutor_id). The rules: each tutor handles a single subject, and each student works with a single tutor for each subject.

tutor_id→subject{stu_id, subject}→tutor_id\text{tutor\_id} \to \text{subject} \qquad \{\text{stu\_id, subject}\} \to \text{tutor\_id}

The keys: {stu_id, subject} and {stu_id, tutor_id}. All three attributes are prime. tutor_id→subject\text{tutor\_id} \to \text{subject}: tutor_id is not a superkey, a BCNF violation. But subject is prime, so 3NF holds. Highest 3NF.

The version with letters: R(A,B,C)R(A,B,C), F={AB→C, C→B}F = \{AB \to C,\ C \to B\}, keys AB and AC, result 3NF.

All together: a worked determination

R(A,B,C,D,E)R(A,B,C,D,E), F={AB→CDE, C→D, D→B}F = \{AB \to CDE,\ C \to D,\ D \to B\}.

Keys:        A is not on the right → in every key
             {A}+ = A
             {A,B}+ = ABCDE   key
             {A,C}+: C -> D, D -> B, AB -> CDE  = ABCDE  key
             {A,D}+: D -> B, AB -> CDE          = ABCDE  key
             {A,E}+ = AE      not a key
Prime:       A, B, C, D        (E is not prime)

AB -> CDE    AB is a superkey          BCNF ok
C  -> D      C is not a superkey       BCNF violation; D is prime → 3NF ok
D  -> B      D is not a superkey       BCNF violation; B is prime → 3NF ok

Result: 3NF (not BCNF)

One more BCNF example: R(A,B,C,D)R(A,B,C,D), F={A→BCD, BC→A}F = \{A \to BCD,\ BC \to A\}. {A}+\{A\}^+ and {B,C}+\{B,C\}^+ are both ABCD: every left side is a superkey, BCNF. The university’s course(course_id, title, dept_name, credits) and department(dept_name, building, budget) tables are in BCNF for the same reason: their only FDs come from the key.

PITFALL · looking only at F in the pieces. When you test the whole schema, looking at the FDs in F is enough. But when you test a decomposed piece, you have to think of the projection of F+F^+ onto that piece: FDs that come through an intermediate attribute not visible in the piece count too. We will use this next week.

Summary table of normal forms

FormConditionWhat breaks it
1NFAtomic valuesA list, a repeating group
2NF1NF + no partial dependencyPart of a key → non-prime
3NFEvery X→AX \to A: X is a superkey or A is primeNon-key → non-prime
BCNFEvery X→YX \to Y: X is a superkeyNon-superkey → anything

Common mistakes

Wrong ideaThe truth
Not looking at the keys before finding the normal formFirst all candidate keys and prime attributes
“A is prime” = A is in the primary keyAppearing in any candidate key is enough
3NF and BCNF are the sameIn 3NF a prime right side is allowed; in BCNF it is not
Several candidate keys break BCNFAB and CD can be two keys, and the schema is still BCNF
Doing the left-side extraneous test with the modified FTake X+X^+ under the original F
The canonical cover is uniqueDifferent but equivalent results can come out depending on the order
If 2NF is broken, it can still be 3NFThe forms are nested: every 3NF schema is in 2NF

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

A table student(stu_id, name, phones) stores values like '0532 111 22 33, 0216 444 55 66' in phones. Which normal form does it violate, and what is the usual fix?

Show answer
1NF; move phones to a separate table student_phone(stu_id, phone)

1NF requires a single, indivisible (atomic) value in each cell. A comma-separated list holds several values. Fix: a separate row for each phone, in a separate table: student_phone(stu_id, phone), with key (stu_id, phone).

Question 2medium

Let F={AB→C, A→B}F = \{AB \to C,\ A \to B\}. Is BB extraneous in AB→CAB \to C? Give the canonical cover.

Show answer
Yes; Fc = {A -> BC}

To test whether B on the left side is extraneous, drop B and compute {A}+\{A\}^+ under the original F: with A→BA \to B we get {A,B}, with AB→CAB \to C {A,B,C}. C is in it, so B is extraneous. The FD becomes A→CA \to C. Combine the same left sides: A→BCA \to BC.

Question 3medium

Let F={A→BC, B→C}F = \{A \to BC,\ B \to C\}. Which attribute is extraneous, and what is the canonical cover?

Show answer
C in A -> BC; Fc = {A -> B, B -> C}

Test C on the right side: put A→BA \to B in place of A→BCA \to BC; under the new set {A→B, B→C}\{A \to B,\ B \to C\}, {A}+={A,B,C}\{A\}^+ = \{A,B,C\}. C still comes out, so it is extraneous. Canonical cover: A→BA \to B, B→CB \to C.

Question 4hard

Find a canonical cover of F={A→C, AC→D, E→AD, E→H}F = \{A \to C,\ AC \to D,\ E \to AD,\ E \to H\}.

Show answer
{A -> CD, E -> AH}

1) Split the right sides: A→C, AC→D, E→A, E→D, E→H.
2) Is C extraneous in AC→D? {A}+\{A\}^+ = {A,C,D} (A→C, then AC→D). Yes: A→D.
3) Redundant FD: if E→D is removed, {E}+\{E\}^+ still contains D via E→A, A→C, A→D. E→D is redundant. The others are needed.
4) Combine: A→CD, E→AH. Check: the new set is equivalent to the original.

Question 5medium

Let R(A,B,C,D)R(A,B,C,D) with F={AB→C, B→D}F = \{AB \to C,\ B \to D\}. What is the highest normal form of RR?

Show answer
1NF

A and B are not on the right, {A,B}+\{A,B\}^+ = ABCD: the only key is AB. The prime attributes are A, B. For B→DB \to D: B is a proper subset of the key and D is not prime. There is a partial dependency, which breaks 2NF. Highest: 1NF.

Question 6medium

Let R(A,B,C,D,E)R(A,B,C,D,E) with F={A→B, B→C, C→D, D→E}F = \{A \to B,\ B \to C,\ C \to D,\ D \to E\}. What is the highest normal form?

Show answer
2NF

The key is a single attribute: A ({A}+\{A\}^+ = ABCDE). A single-attribute key has no proper subset, so there can be no partial dependency: 2NF. But in B→CB \to C, B is not a superkey and C is not prime: a transitive dependency, which breaks 3NF. The answer is 2NF.

Question 7medium

Let R(A,B,C)R(A,B,C) with F={AB→C, C→B}F = \{AB \to C,\ C \to B\}. Is RR in BCNF? Is it in 3NF?

Show answer
Not BCNF; 3NF

Keys: A is not on the right. {A,B}+ = ABC, {A,C}+ = ABC: the keys are AB, AC. For C→BC \to B: C is not a superkey, which violates BCNF. For 3NF there is a second chance: is B prime? B is in the key AB, yes. All FDs satisfy the 3NF condition: 3NF but not BCNF.

Question 8easy

Let R(A,B,C,D)R(A,B,C,D) with F={A→BCD, BC→A}F = \{A \to BCD,\ BC \to A\}. What is the highest normal form?

Show answer
BCNF

{A}+\{A\}^+ = ABCD, {B,C}+\{B,C\}^+ = ABCD. The left sides of both FDs are superkeys. The BCNF condition holds for every FD. The keys are A and BC.

Question 9hard

Let R(A,B,C,D,E)R(A,B,C,D,E) with F={AB→CDE, C→D, D→B}F = \{AB \to CDE,\ C \to D,\ D \to B\}. Find all candidate keys and the highest normal form.

Show answer
Keys AB, AC, AD; 3NF

A is not on the right, so it is in every key. {A}+ = A. AB: everything. AC: C→D, D→B, then AB→CDE, everything. AD: D→B, AB→CDE, everything. AE: {A,E}, not a key. The keys are AB, AC, AD; the prime attributes are A, B, C, D.C→DC \to D: C is not a superkey, a BCNF violation, but D is prime. D→BD \to B: B is prime. In AB→CDE the left side is a superkey. Every FD satisfies 3NF: 3NF, not BCNF.

Question 10medium

Let R(A,B,C,D,E)R(A,B,C,D,E) with F={AB→C, C→D, B→E}F = \{AB \to C,\ C \to D,\ B \to E\}. What is the highest normal form?

Show answer
1NF

A, B are not on the right; {A,B}+ = ABCDE, the only key is AB. B→EB \to E: B, a proper subset of the key, determines the non-prime E. A partial dependency: not 2NF. (C→DC \to D would also break 3NF, but there is no 2NF anyway.)

Question 11easy

Let R(A,B,C,D)R(A,B,C,D) with F={AB→CD, CD→AB}F = \{AB \to CD,\ CD \to AB\}. What is the highest normal form?

Show answer
BCNF

{A,B}+\{A,B\}^+ = ABCD and {C,D}+\{C,D\}^+ = ABCD. The left sides of both FDs are superkeys. BCNF. There are two candidate keys: AB, CD. Having more than one key does not break BCNF.

Question 12easy

True or false: every relation schema with exactly two attributes is in BCNF.

Show answer
True

For R(A,B) the possible non-trivial FDs are A→BA \to B and B→AB \to A. If A→BA \to B holds, {A}+ = AB and A is a superkey. The same goes for B→AB \to A. If there is no FD, the key is AB. In every case the left side is a superkey: BCNF.

Verification: All canonical covers, extraneous-attribute tests, candidate keys and normal form results were computed with verify/SEN2104/lib.mjs (canonicalCover, closure, equivalent, candidateKeys, highestNF; FD projection over all subsets) (verify/SEN2104/10.mjs, 43 checks, 0 errors).

Review card

Canonical cover: 1) split the right sides 2) drop extraneous attributes on the left 3) drop redundant FDs 4) combine equal left sides Is B on the left extraneous (AB->C): does {A}+ under the ORIGINAL F contain C? Is C on the right extraneous (A->BC): does {A}+ under F changed to A->B contain C? The canonical cover is not unique; the order can change the result 1NF: atomic values (no list, no repeating group) 2NF: 1NF + no non-prime attribute depends on a PART of a key 3NF: for every X->A, X is a superkey OR A is prime BCNF: for every X->A, X is a superkey Prime attribute = appears in any candidate key A single-attribute key → automatically 2NF A two-attribute schema → always BCNF Order: find the keys → write the prime attributes → test every FD against BCNF, 3NF, 2NF