Sitemap
Yashdesai

Yashdesai

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:

Press enter or click to view image in full size

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:

  1. Insert Dummy Data:
  • You might insert a dummy employee record with null or fake values to create the Finance department:
Press enter or click to view image in full size

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:

Press enter or click to view image in full size

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:
Press enter or click to view image in full size

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:

Press enter or click to view image in full size

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.
Press enter or click to view image in full size

Summary of the Problems and Solutions

Press enter or click to view image in full size

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:

  1. First Normal Form (1NF)
  2. Second Normal Form (2NF)
  3. Third Normal Form (3NF)
  4. 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:

Press enter or click to view image in full size

Issues:

  • The Courses column contains multiple values (not atomic).

Solution (1NF): Split the courses into individual rows:

Press enter or click to view image in full size

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:

Press enter or click to view image in full size

Here, the composite primary key is (Student_ID, Course_ID), which uniquely identifies each record.

  • The problem is that Instructor only depends on Course_ID, not the entire composite key (Student_ID, Course_ID).
  • This is a partial dependency because Instructor is 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:

  1. Students Table (for student-specific information):
Press enter or click to view image in full size

Courses Table (for course and instructor-specific information):

Press enter or click to view image in full size

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:

  1. It is in Second Normal Form (2NF).
  2. 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:

Press enter or click to view image in full size

Step 1: Identify the Problems

  1. Primary Key: In this table, the primary key could be Student_ID.
  2. Transitive Dependency: The Instructor and Department fields depend on the Course, not directly on Student_ID. For example, Math always has Dr. Smith as its instructor and belongs to the Science department.

Step 2: Convert to 3NF

To eliminate transitive dependencies, we need to split the table into multiple related tables.

Create New Tables

  1. Students Table:
  • This will hold student-specific information.
Press enter or click to view image in full size

Courses Table:

  • This table will hold information about courses, including the instructor and department.
Press enter or click to view image in full size

Step 3: Establish Relationships

Now, we have two tables:

  • Students table with Student_ID, Name, and Course.
  • Courses table with Course, Instructor, and Department.

Benefits of This Structure

  • Reduced Redundancy: Instead of repeating the Instructor and Department information for every student taking the same course, we store it only once in the Courses table.
  • Easier Updates: If the instructor for Math changes, we only need to update it in the Courses table, not in every student record.

Summary of 3NF

  1. No Transitive Dependencies: Every non-key attribute must depend only on the primary key.
  2. Reduced Redundancy: Information is stored in a way that minimizes duplication.
  3. Easier Data Maintenance: Updates are simpler and less prone to errors.