Database Normalization Mastery: Unlocking 1NF to 5NF
Introduction to Normal Forms
Database normalization is a crucial step in designing a robust and scalable database. Normalization helps to eliminate data redundancy, reduce data anomalies, and improve data integrity. In this masterclass, we will delve into the world of normal forms, exploring the concepts of 1NF, 2NF, 3NF, BCNF, 4NF, and 5NF.
First Normal Form (1NF)
A table is said to be in 1NF if it satisfies the following conditions:
- Each cell in the table contains a single value.
- Each column in the table contains atomic values.
- There are no repeating groups or arrays in the table.
In other words, 1NF ensures that each row in the table has a unique combination of values.
Example:
Suppose we have a table called orders that stores information about customer orders.
| OrderID | CustomerName | OrderDate | Items | |---------|--------------|-----------|----------| | 1 | John Smith | 2022-01-01| Book, Pen| | 2 | Jane Doe | 2022-01-15| Book | | 3 | John Smith | 2022-02-01| Pen |
This table is not in 1NF because the Items column contains a list of values. To normalize this table, we can create a separate table called order_items that stores each item separately.
CREATE TABLE orders (
OrderID INT PRIMARY KEY,
CustomerName VARCHAR(255),
OrderDate DATE
);
CREATE TABLE order_items (
OrderID INT,
Item VARCHAR(255),
PRIMARY KEY (OrderID, Item),
FOREIGN KEY (OrderID) REFERENCES orders(OrderID)
);
Second Normal Form (2NF)
A table is said to be in 2NF if it satisfies the following conditions:
- The table is in 1NF.
- Each non-key attribute in the table depends on the entire primary key.
In other words, 2NF ensures that each non-key attribute in the table has a functional dependency on the primary key.
Example:
Suppose we have a table called orders that stores information about customer orders.
| OrderID | CustomerName | OrderDate | TotalCost | |---------|--------------|-----------|-----------| | 1 | John Smith | 2022-01-01| 100 | | 2 | Jane Doe | 2022-01-15| 50 | | 3 | John Smith | 2022-02-01| 75 |
This table is not in 2NF because the TotalCost attribute depends only on the OrderID and not on the entire primary key (OrderID, CustomerName). To normalize this table, we can create a separate table called order_costs that stores the total cost of each order.
CREATE TABLE orders (
OrderID INT PRIMARY KEY,
CustomerName VARCHAR(255),
OrderDate DATE
);
CREATE TABLE order_costs (
OrderID INT PRIMARY KEY,
TotalCost DECIMAL(10, 2),
FOREIGN KEY (OrderID) REFERENCES orders(OrderID)
);
Third Normal Form (3NF)
A table is said to be in 3NF if it satisfies the following conditions:
- The table is in 2NF.
- If a table is in 2NF, and a non-key attribute depends on another non-key attribute, then it should be moved to a separate table.
In other words, 3NF ensures that there are no transitive dependencies in the table.
Example:
Suppose we have a table called employees that stores information about employees.
| EmployeeID | Name | Department | DepartmentHead | |------------|------|------------|----------------| | 1 | John | Sales | Jane | | 2 | Jane | Sales | Jane | | 3 | Joe | Marketing | Sarah |
This table is not in 3NF because the DepartmentHead attribute depends on the Department attribute, which is a non-key attribute. To normalize this table, we can create a separate table called departments that stores the department head.
CREATE TABLE employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(255),
Department VARCHAR(255)
);
CREATE TABLE departments (
Department VARCHAR(255) PRIMARY KEY,
DepartmentHead VARCHAR(255)
);
Boyce-Codd Normal Form (BCNF)
A table is said to be in BCNF if it satisfies the following conditions:
- The table is in 3NF.
- For a table to be in BCNF, a composite key cannot have a non-key attribute that depends on only one of the key attributes.
In other words, BCNF ensures that there are no partial dependencies in the table.
Example:
Suppose we have a table called student_courses that stores information about students and courses.
| StudentID | CourseID | CourseName | StudentName | |-----------|----------|------------|-------------| | 1 | 101 | Math | John | | 1 | 102 | Science | John | | 2 | 101 | Math | Jane |
This table is not in BCNF because the StudentName attribute depends only on the StudentID, which is one of the key attributes. To normalize this table, we can create a separate table called students that stores the student name.
CREATE TABLE students (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(255)
);
CREATE TABLE student_courses (
StudentID INT,
CourseID INT,
CourseName VARCHAR(255),
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID) REFERENCES students(StudentID)
);
Fourth Normal Form (4NF)
A table is said to be in 4NF if it satisfies the following conditions:
- The table is in BCNF.
- A table is in 4NF if it does not have any multi-level dependencies.
In other words, 4NF ensures that there are no multi-level dependencies in the table.
Example:
Suppose we have a table called orders that stores information about orders.
| OrderID | ProductID | ProductName | Quantity | |---------|-----------|-------------|----------| | 1 | 101 | Book | 2 | | 1 | 102 | Pen | 3 | | 2 | 101 | Book | 1 |
This table is not in 4NF because the Quantity attribute depends on both the OrderID and ProductID, which is a multi-level dependency. To normalize this table, we can create a separate table called order_items that stores the quantity of each product.
CREATE TABLE orders (
OrderID INT PRIMARY KEY
);
CREATE TABLE products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(255)
);
CREATE TABLE order_items (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES products(ProductID)
);
Fifth Normal Form (5NF)
A table is said to be in 5NF if it satisfies the following conditions:
- The table is in 4NF.
- A table is in 5NF if it does not have any join dependencies.
In other words, 5NF ensures that there are no join dependencies in the table.
Example:
Suppose we have a table called student_courses that stores information about students and courses.
| StudentID | CourseID | CourseName | StudentName | |-----------|----------|------------|-------------| | 1 | 101 | Math | John | | 1 | 102 | Science | John | | 2 | 101 | Math | Jane |
This table is not in 5NF because the StudentName attribute depends on the join of StudentID and CourseID, which is a join dependency. To normalize this table, we can create separate tables called students and courses that store the student name and course name respectively.
CREATE TABLE students (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(255)
);
CREATE TABLE courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(255)
);
CREATE TABLE student_courses (
StudentID INT,
CourseID INT,
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID) REFERENCES students(StudentID),
FOREIGN KEY (CourseID) REFERENCES courses(CourseID)
);
⚔️ YOUR MISSION
Mission: Normalize a Database
You are given a database schema that contains the following tables:
orders: stores information about orderscustomers: stores information about customersproducts: stores information about products
The orders table has the following columns:
OrderID: unique identifier for each orderCustomerID: foreign key referencing thecustomerstableProductID: foreign key referencing theproductstableQuantity: quantity of the product orderedOrderDate: date the order was placedTotalCost: total cost of the order
The customers table has the following columns:
CustomerID: unique identifier for each customerCustomerName: name of the customerAddress: address of the customer
The products table has the following columns:
ProductID: unique identifier for each productProductName: name of the productPrice: price of the product
Your mission is to normalize the database schema to 5NF.
Read the exercise prompt
CREATE TABLE orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
ProductID INT,
Quantity INT,
OrderDate DATE,
TotalCost DECIMAL(10, 2)
);
CREATE TABLE customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(255),
Address VARCHAR(255)
);
CREATE TABLE products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(255),
Price DECIMAL(10, 2)
);
