5. Designing a database with normalization in mind
5. Designing a database with normalization in mind
Introduction
- Database normalization is the process of organizing data in a database to reduce redundancy and improve data integrity.
- The goal is to design a database schema that is efficient, minimizes data duplication, and ensures consistency.
Steps for Database Design with Normalization
Identify Entities and Relationships
- Determine the main entities (tables) and their relationships.
- Example: Identify entities such as
Students,Courses, andEnrollments.
Define Primary Keys
- Assign unique identifiers (primary keys) for each table.
- Example:
student_idfor theStudentstable andcourse_idfor theCoursestable.
Design Tables and Relationships
- Create tables for each entity, ensuring they meet 1NF.
- Define relationships between tables using foreign keys.
- Example:
Enrollmentstable linkingStudentsandCourseswith foreign keys.
Apply Normalization Rules
- Ensure the database design adheres to 2NF by removing partial dependencies.
- Apply 3NF by eliminating transitive dependencies.
- Example: Separate
DepartmentsfromEmployeesto avoid redundancy.
Review and Refine
- Examine the database design for any remaining redundancy or anomalies.
- Optimize the design for performance and ease of use.
Example
Original Table:
| student_id | student_name | course_id | course_name | instructor_name |
|---|---|---|---|---|
| 1 | Alice | 101 | Math | Prof. Johnson |
| 2 | Bob | 102 | Physics | Prof. Smith |
| 1 | Alice | 102 | Physics | Prof. Smith |
Normalization Steps:
1NF: Ensure atomic values and unique column names.
- Original table already in 1NF.
2NF: Remove partial dependencies.
course_nameandinstructor_namedepend oncourse_id, notstudent_id.
Tables:
Students Table:
student_id student_name 1 Alice 2 Bob Courses Table:
course_id course_name instructor_name 101 Math Prof. Johnson 102 Physics Prof. Smith Enrollments Table:
student_id course_id 1 101 1 102 2 102
3NF: Remove transitive dependencies.
- Already achieved by separating courses from enrollments.