Normalization
In database design, anomalies refer to problems that arise when the structure of a database is not well-designed. These anomalies can lead to data inconsistencies and inefficiencies when performing basic operations like inserting, updating, or deleting records. The three main types of anomalies are:
- Insert Anomaly
- Update Anomaly
- Delete Anomaly
1. Insert Anomaly
Definition: An insert anomaly occurs when we cannot insert data into a database due to missing information or when data needs to be inserted into multiple tables unnecessarily.
Example: Consider a table that stores information about employees and departments:
Now, suppose you want to add a new department called “Finance”, but there are no employees in that department yet.
Problem (Insert Anomaly):
In the current table design, you cannot add the new department Finance without inserting an employee record, because the table requires an Employee_ID and Employee_Name to be filled in. Since there are no employees yet, you might end up doing one of the following:
- Insert Dummy Data:
- You might insert a dummy employee record with null or fake values to create the Finance department:
2. Update Anomaly
An update anomaly occurs when changes in one piece of data require multiple updates across the table, leading to data inconsistency if all instances are not updated properly.
Scenario (Unnormalized Table):
Consider the following unnormalized table where employee and department information are stored together:
Problem (Update Anomaly):
- Now, suppose the Sales department is renamed to Corporate Sales.
- In this design, you need to update the Department_Name in every row where the department is Sales.
- After updating Alice’s record:
3. Delete Anomaly
A delete anomaly occurs when deleting one piece of data inadvertently removes other important data.
Scenario (Unnormalized Table):
Consider the same unnormalized table:
Problem (Delete Anomaly):
- Suppose Bob is the only employee in the Marketing department, and Bob leaves the company.
- When you delete Bob’s record from the table, you also delete all the information about the Marketing department, as it’s stored in the same table as the employees.
Summary of the Problems and Solutions
Normalization
Normalization is a database design process that organizes data to minimize redundancy and dependency.
The goal of normalization is to ensure that the data is stored efficiently, maintaining data integrity and avoiding anomalies during data operations like insertions, updates, and deletions.
Normalization involves dividing a database into two or more tables and defining relationships between them.
Normal Forms
Normalization is generally done in several stages known as normal forms (NF). The most commonly used normal forms are:
- First Normal Form (1NF)
- Second Normal Form (2NF)
- Third Normal Form (3NF)
- Boyce-Codd Normal Form (BCNF)
1. First Normal Form (1NF)
Each column contains atomic (indivisible) values.
Get Yashdesai’s stories in your inbox
Join Medium for free to get updates from this writer.
Example: Consider a table storing information about students and their enrolled courses:
Issues:
- The
Coursescolumn contains multiple values (not atomic).
Solution (1NF): Split the courses into individual rows:
2. Second Normal Form (2NF)
Definition: A table is in 2NF if:
- It is in 1NF.
- All non-key attributes are fully functionally dependent on the primary key (no partial dependency).
Example to Understand 2NF:
Consider the following table that contains information about students, courses, and instructors:
Here, the composite primary key is (Student_ID, Course_ID), which uniquely identifies each record.
- The problem is that
Instructoronly depends onCourse_ID, not the entire composite key (Student_ID,Course_ID). - This is a partial dependency because
Instructoris dependent only on part of the primary key (Course_ID), not the whole key.
Converting to 2NF:
To convert this table into Second Normal Form, we need to remove the partial dependency. We split the table into two tables:
- Students Table (for student-specific information):
Courses Table (for course and instructor-specific information):
In this design, Instructor now depends on Course_ID (a non-composite key), and the student-specific data is separated. This eliminates the partial dependency, and the design is now in 2NF.
3. Third Normal Form (3NF)
A table is in Third Normal Form (3NF) if:
- It is in Second Normal Form (2NF).
- There are no transitive dependencies. This means that non-key attributes should not depend on other non-key attributes.
What is a Transitive Dependency?
A transitive dependency occurs when a non-key attribute depends on another non-key attribute rather than directly depending on the primary key. This can lead to redundancy and potential anomalies.
Let’s start with an example of a Students table that is not in 3NF:
Step 1: Identify the Problems
- Primary Key: In this table, the primary key could be
Student_ID. - Transitive Dependency: The
InstructorandDepartmentfields depend on theCourse, not directly onStudent_ID. For example,Mathalways hasDr. Smithas its instructor and belongs to theSciencedepartment.
Step 2: Convert to 3NF
To eliminate transitive dependencies, we need to split the table into multiple related tables.
Create New Tables
- Students Table:
- This will hold student-specific information.
Courses Table:
- This table will hold information about courses, including the instructor and department.
Step 3: Establish Relationships
Now, we have two tables:
- Students table with
Student_ID,Name, andCourse. - Courses table with
Course,Instructor, andDepartment.
Benefits of This Structure
- Reduced Redundancy: Instead of repeating the
InstructorandDepartmentinformation for every student taking the same course, we store it only once in the Courses table. - Easier Updates: If the instructor for
Mathchanges, we only need to update it in the Courses table, not in every student record.
Summary of 3NF
- No Transitive Dependencies: Every non-key attribute must depend only on the primary key.
- Reduced Redundancy: Information is stored in a way that minimizes duplication.
- Easier Data Maintenance: Updates are simpler and less prone to errors.






