Normalization Forms in SQL with Examples
1. First Normal Form (1NF)
Rules:
- Each column must contain only atomic (indivisible) values.
- Each record must be unique.
Example (Not in 1NF):
| StudentID | Name | Courses |
|-----------|-------|-------------------|
|1 | Alice | Math, Science |
|2 | Bob | English, History |
Converted to 1NF:
| StudentID | Name | Course |
|-----------|-------|-----------|
|1 | Alice | Math |
|1 | Alice | Science |
|2 | Bob | English |
|2 | Bob | History |
2. Second Normal Form (2NF)
Rules:
- Must be in 1NF.
- No partial dependency on primary key.
Violation Example (1NF but not 2NF):
| StudentID | Course | InstructorName |
|-----------|----------|----------------|
|1 | Math | Mr. Smith |
|1 | Science | Ms. Jones |
|2 | English | Mr. Brown |
Decomposed:
StudentCourse Table:
| StudentID | Course |
|-----------|----------|
|1 | Math |
|1 | Science |
|2 | English |
CourseInstructor Table:
| Course | InstructorName |
|----------|----------------|
| Math | Mr. Smith |
| Science | Ms. Jones |
| English | Mr. Brown |
3. Third Normal Form (3NF)
Normalization Forms in SQL with Examples
Rules:
- Must be in 2NF.
- No transitive dependency.
Violation Example:
| StudentID | Name | ZipCode | City |
|-----------|-------|---------|---------------|
|1 | Alice | 10001 | New York |
|2 | Bob | 94105 | San Francisco |
Decomposed:
Student Table:
| StudentID | Name | ZipCode |
|-----------|-------|---------|
|1 | Alice | 10001 |
|2 | Bob | 94105 |
ZipCode Table:
| ZipCode | City |
|---------|---------------|
| 10001 | New York |
| 94105 | San Francisco |
4. Boyce-Codd Normal Form (BCNF)
Rules:
- Every determinant must be a candidate key.
- Stricter than 3NF.
Violation Example:
| Course | Instructor | TimeSlot |
|----------|------------|----------|
| Math | Smith | 9AM |
| Math | Smith | 10AM |
Decomposed:
TimeSlot determines Course
Course determines Instructor
5. Fourth Normal Form (4NF)
Rules:
- Must be in BCNF.
- No multi-valued dependencies.
Violation Example:
| Student | Course | Hobby |
|---------|--------|----------|
| Alice | Math | Painting |
| Alice | Math | Dancing |
Normalization Forms in SQL with Examples
Decomposed:
StudentCourse Table:
| Student | Course |
|---------|--------|
| Alice | Math |
StudentHobby Table:
| Student | Hobby |
|---------|----------|
| Alice | Painting |
| Alice | Dancing |