Sub topic 14
Third Normal Form
Prerequisites and the Main Idea
This chapter assumes that you know candidate keys, functional dependencies (FDs), attribute closure, and Second Normal Form (2NF). A functional dependency \(X \to Y\) means that the value of \(X\) determines the value of \(Y\). A superkey is an attribute set that identifies every row. A prime attribute belongs to at least one candidate key.
Third Normal Form (3NF) removes transitive dependencies. A transitive dependency occurs when a non-key attribute determines another non-key attribute through a key:
\[ \text{Key} \to \text{Non-key}1 \to \text{Non-key}2 \]
Formally, a relation is in 3NF if, for every nontrivial FD \(X \to A\):
- \(X\) is a superkey, or
- \(A\) is a prime attribute.
A dependency is nontrivial when \(A\) is not already contained in \(X\).
Worked Example
Consider:
\[ R(\text{StudentID}, \text{CourseID}, \text{InstructorID}, \text{InstructorOffice}) \]
Suppose the FDs are:
- StudentID, CourseID \(\to\) InstructorID
- InstructorID \(\to\) InstructorOffice
The candidate key is {StudentID, CourseID}. Therefore, StudentID and CourseID are prime attributes. InstructorID and InstructorOffice are non-prime attributes.
The first FD is acceptable because {StudentID, CourseID} is a superkey. The second FD violates 3NF because InstructorID is not a superkey and InstructorOffice is not prime.
The relation may still be in 2NF. InstructorOffice does not depend on only part of the composite key. It depends on InstructorID, which is itself determined by the whole key. This is a transitive dependency.
Decomposing into 3NF
Separate instructor facts from course-enrollment facts:
CREATE TABLE instructor (
instructor_id INT PRIMARY KEY,
instructor_office VARCHAR(20) NOT NULL
);
CREATE TABLE course_offering (
student_id INT NOT NULL,
course_id INT NOT NULL,
instructor_id INT NOT NULL REFERENCES instructor(instructor_id),
PRIMARY KEY (student_id, course_id)
);Now InstructorID determines InstructorOffice in instructor, where InstructorID is the key. The enrollment table stores only the instructor assigned to each student and course. This removes repeated office values and prevents update anomalies.
A 3NF decomposition normally aims to be both lossless and dependency preserving. Lossless means joining the decomposed tables reconstructs the original information without creating false rows. Dependency preserving means the original FDs can be checked using the decomposed tables.
3NF Versus BCNF
Every BCNF relation is in 3NF, but some 3NF relations are not in BCNF. A 3NF decomposition can preserve dependencies more easily. BCNF can remove more redundancy, but its decomposition may fail to preserve some FDs.
Exercise
In R(A, B, C), the FDs are AB -> C and C -> B. The candidate keys are AB and AC. Is R in 3NF?
Show answer
Yes. For C -> B, C is not a superkey, but B is prime because it belongs to candidate key AB.