Sub topic 14
Redundancy and Update Anomalies
This chapter assumes you can read a functional dependency (FD) such as dept_id -> dept_name, and that you know what a primary key is and how to write basic CREATE TABLE, INSERT, UPDATE, and DELETE statements. It covers why some table designs cause bugs, how to spot them from the FDs, and how normalization removes them.
Redundancy: the same fact stored twice
A relation has redundancy when one fact is stored in more than one row. Redundancy is not just wasted space. Every copy must be kept identical, and the database does not know the copies belong together.
Take one table that mixes employee facts and department facts. The FDs are:
emp_id -> emp_name, dept_iddept_id -> dept_name, dept_location
The only candidate key is emp_id. The second FD has a left side, dept_id, that is not a key. That is the source of the trouble: a department's name and location are repeated once per employee in that department.
CREATE TABLE emp_dept (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
dept_id INT,
dept_name VARCHAR(50),
dept_location VARCHAR(50)
);
INSERT INTO emp_dept VALUES
(1, 'Asha', 10, 'Sales', 'Pune'),
(2, 'Ravi', 10, 'Sales', 'Pune'),
(3, 'Meera', 20, 'Research', 'Delhi');Department 10 appears in two rows, so Sales and Pune are each stored twice.
The three anomalies
An anomaly is a problem that a data change causes because of redundancy. There are three kinds.
Update anomaly
Moving Sales to Mumbai requires changing every row for department 10. If one row is missed, the table contradicts itself.
UPDATE emp_dept SET dept_location = 'Mumbai' WHERE emp_id = 1;
-- Row 2 still says Pune. Department 10 now has two locations,
-- which violates dept_id -> dept_location.The database accepts this statement. No declared constraint is broken, because the FD was never declared. The table is now inconsistent anyway.
Insertion anomaly
Suppose the company opens department 30 with no employees yet. A row for it needs an emp_id, but emp_id is the primary key and cannot be NULL.
INSERT INTO emp_dept (emp_id, emp_name, dept_id, dept_name, dept_location)
VALUES (NULL, NULL, 30, 'Legal', 'Chennai');
-- Fails: entity integrity forbids a NULL primary key,
-- and emp_name is NOT NULL as well.A fact about a department cannot be recorded without an unrelated fact about an employee.
Deletion anomaly
Meera is the only employee in department 20. Deleting her row also deletes the only record that Research exists and is in Delhi.
DELETE FROM emp_dept WHERE emp_id = 3;
-- The department 20 information is gone with her.Removing one fact destroys another.
| Anomaly | Triggering operation | What goes wrong | Typical cause |
|---|---|---|---|
| Update | Change a repeated value | Copies disagree | Same fact in many rows |
| Insertion | Add a fact with no partner fact | Forced NULLs or refusal | Two facts share one key |
| Deletion | Remove a row | Unrelated fact lost | Two facts share one key |
Where anomalies come from
The cause is an FD whose left side is not a key. When X -> Y holds but X is not a superkey, one value of X can occur in many rows, so Y repeats. Each anomaly then follows from the repetition:
- Updating
Ymeans touching every row with thatX. - A new
Xvalue cannot be added without a key value from some other fact. - Deleting the last row for an
Xvalue removes theX -> Ypairing.
This is why the normal forms in the following chapters are all stated in terms of FDs and keys. They are tests for the situations shown above.
Removing the anomalies
Split the table so each fact is stored once, and link the parts with a foreign key.
CREATE TABLE department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL,
dept_location VARCHAR(50)
);
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);Now each anomaly is gone:
- Update: one
UPDATEon onedepartmentrow changes the location. - Insertion: department 30 can be inserted with no employees.
- Deletion: deleting Meera leaves department 20 in place.
The split must also be lossless, meaning a join of the parts rebuilds the original table exactly, with no extra or missing rows. Here dept_id is a key of department, so the join is lossless. A later chapter covers this test in detail.
Common mistakes
- Treating redundancy as harmless. The extra copies are the cause of the update anomaly. Storage cost is a minor concern.
- Expecting the DBMS to catch it. An undeclared FD is not enforced. The
UPDATEabove succeeds silently. - Fixing the insertion anomaly with NULLs. Allowing NULL in
emp_idis not possible for a primary key. Even with a surrogate key, NULL-filled rows complicate every query. - Assuming splitting is always safe. A careless split can lose information or lose an FD. Splitting
emp_deptinto(emp_id, emp_name)and(dept_id, dept_name, dept_location)is lossy, because no shared attribute links them. - Over-normalizing. Normalization trades anomalies for joins. Read-heavy systems sometimes keep controlled redundancy on purpose, but then the application must keep the copies consistent.
Exercise
R(OrderID, ProductID, ProductName, Qty) has primary key (OrderID, ProductID) and the FD ProductID -> ProductName. Name one update anomaly, one insertion anomaly, and one deletion anomaly this design causes.
Show answer
Update: a product that appears in many orders has its name stored in every row, so renaming it in one row but not the others leaves inconsistent names. Insertion: a new product that no one has ordered yet cannot be recorded, because OrderID is part of the primary key and cannot be NULL. Deletion: deleting the only order that contains a product also deletes the product's name. The cause is ProductID -> ProductName, whose left side is not a key. Fixing it means moving ProductName into a Product(ProductID, ProductName) table.
Exercise
A table has columns (A, B, C), key A, and the FD B -> C. Why does a deletion anomaly occur, and what decomposition removes it?
Show answer
B is not a key, so many rows can share one B value, and C is stored with each of them. Deleting the last row with a given B value also deletes the fact that this B value determines that C value. Decomposing into R1(B, C) and R2(A, B) removes the anomaly. B is a key of R1, and the join on B rebuilds the original table.