What is Normal Forms and Normalization in MySQL

Normalization in MySQL is a process of structuring a relational database to minimize redundancy and improve data integrity. This guide introduces you to the concept of normalization and how it is applied in MySQL.


What is Normalization?

Normalization involves organizing data in a database into tables and columns in a way that reduces redundancy and dependency. It involves dividing large tables into smaller, interrelated tables and defining relationships between them.

First Normal Form (1NF)

The First Normal Form requires that each table cell should contain a single value, and each record needs to be unique.

Example: Converting a Table to 1NF

Suppose we have an Orders table that violates 1NF because it contains multiple values in the Items column.

Before 1NF:

OrderID Items
1 Pen, Notebook, Eraser
2 Pencil, Eraser

To convert this table to 1NF, we need to ensure that each field contains only atomic (indivisible) values.



After 1NF:

SQL
CREATE TABLE Orders (    OrderID INT,    Item VARCHAR(100)
);

INSERT INTO Orders (OrderID, Item) VALUES
(1, 'Pen'),
(1, 'Notebook'),
(1, 'Eraser'),
(2, 'Pencil'),
(2, 'Eraser');


Each Item is now in its own row, adhering to 1NF.


Second Normal Form (2NF)

A table is in 2NF if it is in 1NF and all non-key columns are fully dependent on the primary key

Example: Converting a Table to 2NF

Let's consider a StudentCourses table that is in 1NF but not in 2NF due to partial dependency.

Before 2NF:

StudentID Course Instructor
101 Math Dr. Smith
101 Science Dr. Jones
102 Math Dr. Smith

The Instructor is dependent only on the Course, not on the composite key (StudentID, Course).


After 2NF:


The Courses table is created to remove the partial dependency, achieving 2NF.


Third Normal Form (3NF)

Example: Converting a Table to 3NF

Consider a Customers table in 2NF but not in 3NF due to transitive dependency.

Before 3NF:

CustomerID Address ZipCode City Country
001 123 Lane 90001 Los Angeles USA
002 456 Avenue 10001 New York USA

The City and Country are transitively dependent on CustomerID through ZipCode.



After 3NF:


The ZipCodes table is created to remove the transitive dependency, adhering to 3NF.