Sub topic 12
Redundancy and Update Anomalies
Assume you know tables, primary keys, foreign keys, and basic functional dependencies. This chapter uses those ideas to explain why poorly designed relations can store the same fact many times.
What Redundancy Means
Redundancy is storing one fact in multiple rows. Consider this relation:
| student_id | student_name | course_id | course_name | instructor |
|---|---|---|---|---|
| S1 | Asha | C10 | Databases | Rao |
| S2 | Ben | C10 | Databases | Rao |
| S3 | Chen | C20 | Networks | Iyer |
The fact that course C10 is called Databases and is taught by Rao appears once for every enrolled student. The relation has the functional dependencies:
- studentid determines studentname
- courseid determines coursename and instructor
- studentid, courseid determines the enrollment row
The relation’s candidate key is studentid, courseid. However, course_id alone determines some non-key attributes. This partial dependency creates repeated course data.
The Three Anomalies
Update Anomaly
An update anomaly occurs when one fact must be changed in several rows. If Rao’s name changes, every row for course C10 must be updated. If one row is missed, the database contains conflicting values for the same instructor.
Insertion Anomaly
An insertion anomaly occurs when a fact cannot be inserted without an unrelated fact. In the example, you cannot add a new course before any student enrolls unless student columns are allowed to be null or a placeholder row is used. Both choices weaken the design.
Deletion Anomaly
A deletion anomaly occurs when deleting one fact accidentally removes another. If S3 is the only student enrolled in C20, deleting S3’s enrollment also deletes the only stored record that Networks exists and that Iyer teaches it.
CREATE TABLE course (
course_id INT PRIMARY KEY,
course_name VARCHAR(50) NOT NULL,
instructor VARCHAR(50) NOT NULL
);
CREATE TABLE enrollment (
student_id INT,
course_id INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (course_id) REFERENCES course(course_id)
);This decomposition stores each course fact once. Enrollment records now represent only the relationship between a student and a course.
How Normalization Helps
Normalization decomposes a relation into smaller relations according to its dependencies. The goal is to reduce redundancy while preserving meaning.
A useful diagnostic sequence is:
- Identify the candidate keys.
- List which attributes depend on which determinants.
- Separate facts that describe different entities or relationships.
- Check that joining the decomposed relations reconstructs the original information.
The decomposition above addresses the partial dependency on course_id. Higher normal forms address other dependencies, such as transitive dependencies and determinants that are not candidate keys.
Exercise
If courseid determines coursename and instructor, why is repeating those values for every enrollment risky?
Show answer
A course fact may need multiple updates, may be impossible to insert before enrollment, and may disappear when the last enrollment is deleted.