Insertion, update and deletion anomalies
Overview
An anomaly occurs when several independent facts are forced into the same row structure. The redundant training enrollment relation stores course titles and instructor names repeatedly. Changing only one copy can produce conflicting facts; a new course cannot be stored before a learner enrolls; deleting the last enrollment can erase the only stored course information. Load fd-enrollment.sql. The two examples preview hypothetical changes using read-only queries.
First example
WITH preview AS ( SELECT course_id, CASE WHEN learner_id=1 AND course_id=10 THEN 'SQL Revised' ELSE course_title END AS title FROM fd_enrollment)SELECT course_id, COUNT(DISTINCT title)FROM preview GROUP BY course_idHAVING COUNT(DISTINCT title)>1ORDER BY course_id;Observed SQLite result (rows in JSON):
[ [ 10, 2 ]]How it works
The first query changes the displayed title for enrollment (1,10) only, leaving course 10 with two distinct titles. The second filters out all enrollments of learner 3; course 12 disappears from the projected course catalog. The edge tests reproduce these changes and a rejected insert with missing learner ID in disposable databases. A design correction is to separate Learner(L,N), Instructor(I,M), Course(C,T,I) and Enrollment(L,C,S) with primary and foreign keys. A course can then exist without enrollments. This reconstructs the original rows via key-based joins for the stated rules and preserves their dependencies. The next normalization chapter formalizes losslessness and dependency preservation. Do not use UNIQUE(course_id) on the flat table; it would prohibit legitimate multiple enrollments instead of enforcing C→T alone.
Practical example
SELECT DISTINCT course_id,course_titleFROM fd_enrollmentWHERE learner_id<>3ORDER BY course_id;Observed SQLite result (rows in JSON):
[ [ 10, "SQL Basics" ], [ 11, "SQL Reports" ]]Practice and mistakes
Practice: simulate removing all enrollments for course 10. Identify the lost course fact and contrast it with deleting only matching enrollment rows in the decomposed model. Never run destructive exercises on an application database. Use the preceding ER chapter and the next normalization chapter for the design.