Quiz 2

16 - Normalization: 1NF, 2NF, 3NF

2392 words
12 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# 16 - Normalization: 1NF, 2NF, 3NF ## 🎯 Learning Objectives After reading this topic, you will be able to: - Identify insertion, deletion, and update anomalies - Define and apply 1NF, 2NF, and 3NF - Distinguish between prime and non-prime attributes - Detect partial dependencies and transitive dependencies - Norma...

16 - Normalization: 1NF, 2NF, 3NF

🎯 Learning Objectives

After reading this topic, you will be able to:
  • Identify insertion, deletion, and update anomalies
  • Define and apply 1NF, 2NF, and 3NF
  • Distinguish between prime and non-prime attributes
  • Detect partial dependencies and transitive dependencies
  • Normalize a relation up to 3NF

📋 Prerequisites

📖 Core Content

16.1 Intuition: Why Normalize?

Imagine tracking employee projects in a single table:
emp_idemp_namedeptprojectproject_budget
101AliceCSAlpha50000
101AliceCSBeta30000
102BobMathGamma20000
Problems:
  1. Update anomaly: If Alice changes departments, we must update EVERY row with emp_id=101
  2. Insertion anomaly: Can't add a new department without assigning an employee
  3. Deletion anomaly: If Bob leaves, we lose info about Project Gamma Normalization is the process of decomposing tables to eliminate these anomalies. It's a series of tests (normal forms) that a schema can pass or fail.
Why This Matters: Normalization is the core of good database design (~13% of exam questions). A non-normalized database leads to data inconsistencies and maintenance nightmares.

16.2 Anomalies in Detail

AnomalyDefinitionExample
InsertionCan't insert data without additional unrelated dataCan't add a department without an employee
DeletionDeleting data removes unintended informationDeleting Bob deletes Project Gamma info
UpdateChanging data requires multiple row updatesChanging Alice's department requires 2 updates

16.3 The Normal Forms Hierarchy

(Diagram) Each normal form is a superset of the previous one:
5NF4NFBCNF3NF2NF1NF5NF \subset 4NF \subset BCNF \subset 3NF \subset 2NF \subset 1NF

16.4 Key Terminology

Before understanding normal forms, master these terms:
TermDefinitionExample
Prime attributeAn attribute that is part of ANY candidate keyIn R(A,B,C) with CK {A,B}, A and B are prime
Non-prime attributeAn attribute NOT part of any candidate keyC is non-prime
Partial dependencyA non-prime attribute depends on part of a candidate keyIn R(A,B,C) with CK {A,B}, A→C would be partial
Transitive dependencyA non-prime attribute depends on another non-prime attributeA → B and B → C means C is transitively dependent on A

16.5 First Normal Form (1NF)

Definition: A relation is in 1NF if every attribute has atomic (indivisible) values. Violation: A table with a column containing multiple values (like a list). Bad (not 1NF):
idnamephones
1Alice555-1234, 555-5678
Good (1NF):
idnamephone
1Alice555-1234
1Alice555-5678
Important: In DBMS theory, we assume all relations are in 1NF by default. Multi-valued attributes are not allowed in the pure relational model.

16.6 Second Normal Form (2NF)

Definition: A relation is in 2NF if:
  1. It is in 1NF
  2. No non-prime attribute is partially dependent on any candidate key Check: For every candidate key CK and every proper subset S ⊂ CK, there is no FD S → A where A is non-prime.

Worked Example: 2NF Check

R(A,B,C,D)R(A, B, C, D) with F={ABC,AD}F = \{AB \rightarrow C, A \rightarrow D\}. Candidate keys? Compute {AB}+\{AB\}^+ = {A, B, C, D} = R. {A}+\{A\}^+ = {A, D} ≠ R. {B}+\{B\}^+ = {B} ≠ R. So CK = {A, B}. Check partial dependencies:
  • ADA \rightarrow D: A ⊂ CK {A, B}, and D is non-prime. This is a partial dependency!
  • ABCAB \rightarrow C: LHS = full CK, not a subset. Not partial. Since ADA \rightarrow D is a partial dependency, R is NOT in 2NF. Fix: Decompose R into:
  • R1(A,D)R_1(A, D) — covers the partial dependency
  • R2(A,B,C)R_2(A, B, C) — the remaining attributes with the original CK Now both R1R_1 and R2R_2 are in 2NF.

Example 2: 2NF Check

R(A,B,C,D)R(A, B, C, D) with F={ABC,BD}F = \{AB \rightarrow C, B \rightarrow D\}. Candidate key: {A, B} (assuming A+=AA^+ = {A}, B+=BB^+ = {B}, AB+=A,B,C,DAB^+ = {A,B,C,D}). Check partial dependencies:
  • BDB \rightarrow D: B ⊂ CK {A, B}, D is non-prime. Partial dependency!
  • R is NOT in 2NF.

16.7 Third Normal Form (3NF)

Definition: A relation is in 3NF if:
  1. It is in 2NF
  2. No non-prime attribute is transitively dependent on any candidate key OR: For every non-trivial FD XAX \rightarrow A, either:
    • XX is a superkey, OR
    • AA is a prime attribute (part of some candidate key) Alternative definition: A relation is in 3NF if for every non-trivial FD XAX \rightarrow A:
X is a superkeyA is a prime attributeX \text{ is a superkey} \lor A \text{ is a prime attribute}

Worked Example: 3NF Check

R(A,B,C,D)R(A, B, C, D) with F={AB,BC}F = \{A \rightarrow B, B \rightarrow C\}. Candidate key: {A, D} (since D doesn't appear on RHS, and {A,D}+\{A, D\}^+ = {A, B, C, D}). Check transitive dependency:
  • ABA \rightarrow B: A is not a superkey (A+A^+ = {A,B,C} ≠ R). B is non-prime. Violation of 3NF!
  • BCB \rightarrow C: B is not a superkey (B+B^+ = {B,C} ≠ R). C is non-prime. Violation! R is NOT in 3NF. Fix: Decompose into:
  • R1(A,B,C)R_1(A, B, C) — covers the FDs (PK = A)
  • R2(A,D)R_2(A, D) — has the original CK Now check 3NF for the decomposition:
  • R1(A,B,C)R_1(A, B, C) with F={AB,BC}F = \{A \rightarrow B, B \rightarrow C\}:
    • CK = {A}.
    • ABA \rightarrow B: A is a superkey. ✓
    • BCB \rightarrow C: B is not a superkey, C is non-prime. Still violates 3NF! Need to further decompose R1R_1:
    • R11(A,B)R_{11}(A, B) — covers ABA \rightarrow B
    • R12(B,C)R_{12}(B, C) — covers BCB \rightarrow C

Example 2: 3NF Check

R(A,B,C)R(A, B, C) with F={AB,BC}F = \{A \rightarrow B, B \rightarrow C\}. CK = {A}. Check:
  • ABA \rightarrow B: A is a superkey. ✓
  • BCB \rightarrow C: B is NOT a superkey (B+B^+ = {B,C} ≠ R). C is non-prime. Violation! Not in 3NF. Decompose to R1(A,B)R_1(A, B) and R2(B,C)R_2(B, C).

Example 3: 3NF with Prime Attribute on RHS

R(A,B,C)R(A, B, C) with F={ABC,CA}F = \{AB \rightarrow C, C \rightarrow A\}. Candidate keys: {A, B} (since AB+AB^+ = {A,B,C}) and {C, B} (since {C,B}+\{C,B\}^+ = {A,B,C}). Prime attributes: A, B, C (all are prime!). Check:
  • ABCAB \rightarrow C: AB is a superkey. ✓
  • CAC \rightarrow A: C is not a superkey (C+C^+ = {A,C} ≠ R). BUT A is a prime attribute (part of {A,B}). This is allowed in 3NF! So R IS in 3NF (but NOT in BCNF — that's the distinction we'll see later).

16.8 Normalization: Step-by-Step Process

(Diagram)

16.9 Worked Example: Complete Normalization

Given: R(A,B,C,D,E)R(A, B, C, D, E) with F={ABC,AD,CE}F = \{AB \rightarrow C, A \rightarrow D, C \rightarrow E\} Step 1: Find candidate keys.
  • Attributes not on RHS: A, B (both appear on LHS only)
  • {A,B}+\{A, B\}^+: {A,B} → ABCAB \rightarrow C → {A,B,C} → CEC \rightarrow E → {A,B,C,E} → ADA \rightarrow D → {A,B,C,D,E} = R
  • CK = {A, B}. Prime: A, B. Non-prime: C, D, E. Step 2: Check 2NF.
  • ADA \rightarrow D: A ⊂ CK, D non-prime. Partial dependency!
  • Not in 2NF. Step 3: Decompose to 2NF.
  • R1(A,D)R_1(A, D) — covers ADA \rightarrow D
  • R2(A,B,C,E)R_2(A, B, C, E) — remaining Step 4: Check R2R_2 for 2NF. CK of R2R_2: {A, B}. FDs in R2R_2: ABCAB \rightarrow C, CEC \rightarrow E.
  • ABCAB \rightarrow C: Full CK. Not partial. ✓
  • CEC \rightarrow E: C is not a subset of CK. Not partial. ✓ No partial dependencies. R2R_2 is in 2NF. Step 5: Check R2R_2 for 3NF. CK = {A, B}. Prime: A, B. Non-prime: C, E.
  • ABCAB \rightarrow C: AB is a superkey. ✓
  • CEC \rightarrow E: C is NOT a superkey (C+C^+ = {C,E} in R2R_2). E is non-prime. Transitive dependency! Step 6: Decompose R2R_2 to 3NF.
  • R21(C,E)R_{21}(C, E) — covers CEC \rightarrow E
  • R22(A,B,C)R_{22}(A, B, C) — remaining Final 3NF decomposition: R1(A,D)R_1(A, D), R21(C,E)R_{21}(C, E), R22(A,B,C)R_{22}(A, B, C) All three relations are in 3NF (and also in 2NF and 1NF).

📐 Key Formulas / Concepts

Normal FormConditionViolation Example
1NFAll attributes are atomicMulti-valued phone numbers
2NF1NF + No partial dependency (non-prime → part of CK)ADA \rightarrow D where CK={A,B}
3NF2NF + No transitive dependency (non-prime → non-prime) OR XAX \rightarrow A where XX is superkey or AA is primeCEC \rightarrow E where C isn't a superkey and E is non-prime

⚠️ Common Pitfalls

Pitfall 1: Checking 2NF Against Only the Primary Key

The Mistake: Only checking partial dependencies against the primary key, ignoring other candidate keys. Why It's Wrong: 2NF concerns ALL candidate keys, not just the chosen primary key. Fix: Find ALL candidate keys first. Check if any proper subset of any candidate key determines a non-prime attribute.

Pitfall 2: Confusing 3NF and BCNF

The Mistake: Thinking 3NF requires that every LHS is a superkey (that's BCNF). Why It's Wrong: 3NF is weaker — it also allows FDs where the RHS is a prime attribute, even if the LHS isn't a superkey. Memory Aid: 3NF = "Superkey or Prime attribute on RHS"

Pitfall 3: Not Identifying All Candidate Keys First

The Mistake: Jumping to normalization without finding all candidate keys. Why It's Wrong: You can't check for partial/transitive dependencies without knowing what the candidate keys are. If you miss a candidate key, you might miss a violation. Fix: Always compute all candidate keys using attribute closure before checking normal forms.

📝 Practice Questions

Q1. Define the three types of anomalies that normalization addresses.

Answer
  1. Insertion anomaly: Cannot insert a new row without providing unrelated data (e.g., can't add a new department without assigning an employee)
  2. Deletion anomaly: Deleting a row deletes unintended information (e.g., deleting an employee also deletes the department info)
  3. Update anomaly: Changing a value requires updating multiple rows (e.g., changing a department name requires updating every employee row)

Q2. What is 1NF? Give an example of a violation.

Answer
1NF requires all attributes to have atomic (indivisible) values.
Violation: A table where an attribute can contain multiple values:
student_idcourses
1CS101, MA101
Fix: Create separate rows for each course:
student_idcourse
1CS101
1MA101

Q3. What is a partial dependency? When does it violate 2NF?

Answer
A partial dependency occurs when a non-prime attribute depends on only part of a candidate key (a proper subset).
Violation: In R(A,B,C,D)R(A, B, C, D) with CK = {A, B} and FD ADA \rightarrow D:
  • D is non-prime, A is a proper subset of CK
  • This is a partial dependency → R is NOT in 2NF
Fix: Decompose into R1(A,D)R_1(A, D) and R2(A,B,C)R_2(A, B, C).

Q4. Check if R(A,B,C)R(A, B, C) with F={AB,BC}F = \{A \rightarrow B, B \rightarrow C\} is in 3NF.

Answer
Candidate key: {A}
Prime attributes: A. Non-prime: B, C.
Check 2NF: CK = {A} has no proper subset, so no partial dependency. ✓
Check 3NF:
  • ABA \rightarrow B: A is a superkey. ✓
  • BCB \rightarrow C: B is NOT a superkey (B+B^+ = {B,C}). C is non-prime. Violation!
R is in 2NF but NOT in 3NF. Decompose to R1(A,B)R_1(A, B) and R2(B,C)R_2(B, C).

Q5. Is R(A,B,C)R(A, B, C) with F={ABC,CB}F = \{AB \rightarrow C, C \rightarrow B\} in 3NF?

Answer
Find candidate keys:
  • {A,B}+\{A, B\}^+ = {A,B,C} = R. So {A,B} is a CK.
  • {A,C}+\{A, C\}^+: {A,C} → CBC \rightarrow B → {A,B,C} = R. So {A,C} is also a CK.
Prime: A, B, C (all are prime — every attribute is in some candidate key).
Check 3NF (alternative definition):
  • ABCAB \rightarrow C: AB is a superkey. ✓
  • CBC \rightarrow B: C is not a superkey (C+C^+ = {B,C} ≠ R). BUT B is a prime attribute. ✓
R IS in 3NF (but NOT in BCNF, as we'll see later).

Q6. What is the difference between prime and non-prime attributes?

Answer
  • Prime attribute: An attribute that is part of ANY candidate key for the relation
  • Non-prime attribute: An attribute that is NOT part of any candidate key
Example: In R(A,B,C,D)R(A, B, C, D) with candidate key {A, B}:
  • Prime: A, B
  • Non-prime: C, D

Q7. Normalize R(A,B,C,D)R(A, B, C, D) with F={AB,ABC,BD}F = \{A \rightarrow B, AB \rightarrow C, B \rightarrow D\} to 3NF.

Answer
Step 1: Find candidate keys.
  • Attributes not on RHS: A (appears only on LHS)
  • {A}+\{A\}^+: {A} → ABA \rightarrow B → {A,B} → BDB \rightarrow D → {A,B,D} → ABCAB \rightarrow C → {A,B,C,D} = R
  • CK = {A}. Prime: A. Non-prime: B, C, D.
Step 2: Check 2NF (no partial dependency since CK is single attribute). ✓
Step 3: Check 3NF.
  • ABA \rightarrow B: A is a superkey. ✓
  • ABCAB \rightarrow C: AB is a superkey (contains A). ✓
  • BDB \rightarrow D: B is NOT a superkey (B+B^+ = {B,D}). D is non-prime. Transitive dependency!
Step 4: Decompose.
  • R1(B,D)R_1(B, D) — covers BDB \rightarrow D
  • R2(A,B,C)R_2(A, B, C) — remaining (covers ABA \rightarrow B and ABCAB \rightarrow C)
Final in 3NF: R1(B,D),R2(A,B,C)R_1(B, D), R_2(A, B, C)

Q8. Explain transitive dependency with an example.

Answer
A transitive dependency occurs when a non-prime attribute depends on another non-prime attribute through a chain of FDs.
Example: In R(A,B,C)R(A, B, C) with F={AB,BC}F = \{A \rightarrow B, B \rightarrow C\}, CK = {A}:
  • ABA \rightarrow B: fine (A is superkey)
  • BCB \rightarrow C: B → C where B is non-prime and C is non-prime
  • This forms a transitive dependency: ABCA \rightarrow B \rightarrow C
To break it, decompose into R1(A,B)R_1(A, B) and R2(B,C)R_2(B, C).

🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.