0% found this document useful (0 votes)
12 views

Normalization

Database Management System

Uploaded by

radhikaarora366
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Normalization

Database Management System

Uploaded by

radhikaarora366
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Normalization in a MySQL database involves organizing tables and their columns to reduce

redundancy and improve data integrity. There are several normal forms (1NF, 2NF, 3NF,
etc.) to achieve normalization. Here’s a step-by-step guide to normalize a database in
MySQL:
Example Scenario
Let's consider a database for a library with the following unnormalized table Books:

| BookID | BookTitle | AuthorName | AuthorCountry | PublisherName |


PublisherAddress | ISBN | Price |
|--------|---------------|------------|---------------|---------------|------------------|-------------|-------|
|1 | Book A | Author A | USA | Publisher X | Address X | 123-A |
10.00 |
|2 | Book B | Author B | UK | Publisher Y | Address Y | 456-B |
15.00 |
|3 | Book A | Author A | USA | Publisher Z | Address Z | 789-C |
12.00 |
This table has redundant data, and normalization can improve it.

Step 1: First Normal Form (1NF) – Eliminate Repeating Groups


 Remove duplicate rows and ensure that all entries in columns are atomic (single-
valued).
 Split multi-valued columns into separate rows.
In the example:
 Each attribute should contain only one value.
Original Table:
CREATE TABLE Books (
BookID INT PRIMARY KEY,
BookTitle VARCHAR(100),AuthorName VARCHAR(100),
AuthorCountry VARCHAR(100),PublisherName VARCHAR(100),
PublisherAddress VARCHAR(255),ISBN VARCHAR(20),Price DECIMAL(5, 2)
);
After 1NF:
 No changes required if the original data is already atomic.
Step 2: Second Normal Form (2NF) – Remove Partial Dependencies
 Ensure that all non-primary key attributes are fully dependent on the entire primary
key.
 Split the table if there are partial dependencies (where non-key columns depend on
part of a composite key).

In the example:
 PublisherName and PublisherAddress only depend on Publisher, not on the Book.
 We split the table into two: Books and Publishers.

New Tables:
CREATE TABLE Publishers (
PublisherID INT PRIMARY KEY AUTO_INCREMENT,
PublisherName VARCHAR(100),
PublisherAddress VARCHAR(255)
);

CREATE TABLE Books (


BookID INT PRIMARY KEY,
BookTitle VARCHAR(100),
AuthorName VARCHAR(100),
AuthorCountry VARCHAR(100),
PublisherID INT,
ISBN VARCHAR(20),
Price DECIMAL(5, 2),
FOREIGN KEY (PublisherID) REFERENCES Publishers(PublisherID)
);

Step 3: Third Normal Form (3NF) – Remove Transitive Dependencies


 Ensure that non-primary key attributes do not depend on other non-primary key
attributes.

In the Books table, AuthorCountry depends on AuthorName, not on the primary key BookID.
Therefore, we split the author details into a separate table.

In the example: Create a new Authors table.


New Tables:
CREATE TABLE Authors (
AuthorID INT PRIMARY KEY AUTO_INCREMENT,
AuthorName VARCHAR(100),
AuthorCountry VARCHAR(100)
);

CREATE TABLE Books (


BookID INT PRIMARY KEY,
BookTitle VARCHAR(100),
AuthorID INT,
PublisherID INT,
ISBN VARCHAR(20),
Price DECIMAL(5, 2),
FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID),
FOREIGN KEY (PublisherID) REFERENCES Publishers(PublisherID)
);

Step 4: Boyce-Codd Normal Form (BCNF) (Optional)


 BCNF is an extension of 3NF that ensures stricter normalization.
 Ensure that no non-trivial functional dependency exists, except when a superkey is
involved.
 If your tables already meet 3NF, in most practical scenarios, they may already be in
BCNF.

Step 5: Testing the Normalized Structure


Once the tables are created, you can insert data and query them to ensure normalization is
successful.
Example of inserting data:

INSERT INTO Authors (AuthorName, AuthorCountry)


VALUES ('Author A', 'USA'), ('Author B', 'UK');

INSERT INTO Publishers (PublisherName, PublisherAddress)


VALUES ('Publisher X', 'Address X'), ('Publisher Y', 'Address Y');

INSERT INTO Books (BookTitle, AuthorID, PublisherID, ISBN, Price)


VALUES ('Book A', 1, 1, '123-A', 10.00), ('Book B', 2, 2, '456-B', 15.00);

Step 6: Querying the Normalized Database


You can use JOIN queries to fetch data from the normalized tables.

Example:
SELECT Books.BookTitle, Authors.AuthorName, Publishers.PublisherName
FROM Books
JOIN Authors ON Books.AuthorID = Authors.AuthorID
JOIN Publishers ON Books.PublisherID = Publishers.PublisherID;

Conclusion:
By following the steps above, you can achieve database normalization in MySQL, which
helps in improving data integrity, minimizing redundancy, and organizing your database
efficiently.

You might also like