Handbooks / DBMS / Chapter 3
ER Model and Normalization
7 pages · ~5 min✓ Reviewed
Builds on Relational Model and Relational Algebra. Next up: Views, Stored Procedures and Triggers.
DBMSPage 1ER Model Mental Model
1. Entities and Attributes
- Entity = thing you store facts about (Student)
- Entity set = all such things; rectangle in ER, rows in a table
2. Weak Entities and Relationships
- Weak entity: no key of its own
- Depends on an owner via the identifying relationship
- Key = owner key + partial key (Dependent of Employee)
- Relationship links entities (Student enrolls in Course)
- Relationships can carry attributes:
grade on enrolls
3. ER to Tables
- Entity→ table
- Multivalued attr→ separate table
- M:N relationship→ junction table
sqlCREATE TABLE enrolls (
student_id INT REFERENCES student,
course_id INT REFERENCES course,
grade CHAR(1),
PRIMARY KEY (student_id, course_id));
junction: both FKs, plus the relationship attribute
4. Interview Point
- Why can't Dependent stand alone? No own key; needs Employee key + partial key
- How is M:N stored? Junction table with both FKs;
grade lives there
DBMSPage 2Cardinality and Participation
- Junction PK is usually the pair of FKs, so a pair cannot repeat
sqlCREATE TABLE enrolls (
student_id INT REFERENCES student,
course_id INT REFERENCES course,
PRIMARY KEY (student_id, course_id)
);
2. Participation
- Total (double line): every instance takes part, FK NOT NULL
- Partial: taking part is optional, FK nullable
- In 1:1, put the FK on the side with total participation
3. Common Mistakes
- M:N stored as a comma-separated list column
DBMSPage 3Keys: Super, Candidate, Primary, Foreign
1. The key ladder
- Super key: any set that uniquely identifies a row
- Supersets of a key also qualify: {id}, {id, name}
- Candidate key: minimal super key; a table may have several
- Prime attribute: part of some candidate key
- Super keysall unique sets
- Candidate keysminimal
- Primary keyone chosen
2. Primary, alternate, foreign
DBMSPage 4Functional Dependencies and Closure
1. Meaning of an FD
- X → Y: equal X ⇒ equal Y in any two rows
- A rule about the schema, not the current data
2. Armstrong's axioms and derived rules
3. Closure and keys
- StartX⁺ = X
- Growadd RHS of FDs whose LHS ⊆ set
- Repeatuntil nothing changes
- Super keyX⁺ = all attributes
- Attributes never on any RHS are in every key
- Test them by closure, then add attributes only as needed
4. Partial vs transitive
DBMSPage 5Normal Forms and Anomalies
1. Anomalies in One Wide Table
- Insert: can't add a course without a student
- Update: change many rows, miss some
- Delete: removing the last student loses the course
- 2NF only matters for composite keys
- BCNF is stricter; can lose dependency preservation
3. Decompose
- Offending FDdept_id→dept_name
- New table(X, Y)
- Keep Xin original
- Checklossless join
DBMSPage 6Normalization Cheat Sheet
1. Keys and Cardinality
- Super key ⊇ candidate key ⊇ primary key
- FK points at a key; may be NULL and duplicated
- M:N stored as a comma-separated list column
- FK placed on the 1 side of a 1:N
2. Closure and Normal Form Tests
- X is a super key iff X⁺ contains every attribute
- X⁺: add the RHS of any FD whose LHS is inside the set
R(A,B,C) has A→B and B→C. Find A⁺. Is R in 3NF?
- A⁺ = {A,B,C}, so A is the key
- B→C: B is not a super key and C is non-prime
- Transitive dependency, so not 3NF
Enroll(student_id, course_id, course_name) has key (student_id, course_id) and course_id→course_name. Highest normal form?
- 1NF only
- course_name depends on part of the key (partial dependency)
- Fix: move (course_id, course_name) to a Course table
R(A,B,C) has AB→C and C→B. In 3NF? In BCNF?
- Keys are AB and AC, so A, B, C are all prime
- 3NF: yes, B in C→B is prime
- BCNF: no, C is not a super key
Student–Course is M:N. Why is a course_id column in Student wrong?
- A student row can hold only one course
- Use a junction table with PK (student_id, course_id)