To normalize the provided books table up to the Third Normal Form (3NF), we'll follow these steps:
- First Normal Form (1NF)
- Second Normal Form (2NF)
- Third Normal Form (3NF)
Let's go through each normalization step in detail.
Initial Table Structure
Table: books
| title_id | title_name | retail_price | location_1 | location_2 | location_3 |
|---|
| 102 | 200 Years of German Humor | 19.95 | New York | San Francisco | (NULL) |
| 103 | Ask Your System Administrator | 39.95 | Chicago | (NULL) | (NULL) |
| 104 | But I Did It Unconsciously | 12.99 | Miami | London | New York |
Issues Identified:
- Repeating Groups:
location_1, location_2, location_3 contain multiple values for a single book entry.
- Redundancy: Locations are repeated across multiple records.
- Potential Update Anomalies: Difficulty in adding or removing locations without altering the table structure.
1. First Normal Form (1NF)
Objective: Eliminate repeating groups to ensure that each field contains only atomic (indivisible) values.
Transformation Steps:
- Remove Repeating Groups: Instead of having multiple
location fields, create separate records for each location associated with a book.
1NF Table Structure
Table: books_1NF
| title_id | title_name | retail_price | location |
|---|
| 102 | 200 Years of German Humor | 19.95 | New York |
| 102 | 200 Years of German Humor | 19.95 | San Francisco |
| 103 | Ask Your System Administrator | 39.95 | Chicago |
| 104 | But I Did It Unconsciously | 12.99 | Miami |
| 104 | But I Did It Unconsciously | 12.99 | London |
| 104 | But I Did It Unconsciously | 12.99 | New York |
Key Points:
- Each row now represents a single
location for a book.
- The table adheres to 1NF by ensuring atomicity of values.
2. Second Normal Form (2NF)
Objective: Remove partial dependencies; ensure that all non-key attributes are fully functionally dependent on the primary key.
Assumptions:
- Primary Key Identification:
- In
books_1NF, the primary key could be a composite key: (title_id, location) since a book can be in multiple locations.
Transformation Steps:
- Decompose the Table: Split the table into two tables to eliminate partial dependencies.
2NF Table Structures
-
Table: Books
| title_id | title_name | retail_price |
|---|
| 102 | 200 Years of German Humor | 19.95 |
| 103 | Ask Your System Administrator | 39.95 |
| 104 | But I Did It Unconsciously | 12.99 |
-
Table: Book_Locations
| title_id | location |
|---|
| 102 | New York |
| 102 | San Francisco |
| 103 | Chicago |
| 104 | Miami |
| 104 | London |
| 104 | New York |
Key Points:
-
Books Table:
- Contains information unique to each book.
title_id serves as the primary key.
-
Book_Locations Table:
- Establishes a relationship between books and their storage locations.
- The composite primary key is
(title_id, location).
3. Third Normal Form (3NF)
Objective: Remove transitive dependencies; ensure that non-key attributes are not dependent on other non-key attributes.
Analysis:
-
Books Table:
- Attributes:
title_id, title_name, retail_price
- No transitive dependencies since
title_name and retail_price depend solely on title_id.
-
Book_Locations Table:
- Attributes:
title_id, location
- No transitive dependencies as both attributes form the composite primary key.
Potential Enhancement:
To further reduce redundancy, especially if multiple books are stored in the same locations, we can introduce a separate Locations table. This is optional but recommended for better data integrity and to handle more complex scenarios.
3NF Table Structures
-
Table: Books
| title_id | title_name | retail_price |
|---|
| 102 | 200 Years of German Humor | 19.95 |
| 103 | Ask Your System Administrator | 39.95 |
| 104 | But I Did It Unconsciously | 12.99 |
-
Table: Locations
| location_id | city_name |
|---|
| 1 | New York |
| 2 | San Francisco |
| 3 | Chicago |
| 4 | Miami |
| 5 | London |
-
Table: Book_Locations
| title_id | location_id |
|---|
| 102 | 1 |
| 102 | 2 |
| 103 | 3 |
| 104 | 4 |
| 104 | 5 |
| 104 | 1 |
Key Points:
-
Locations Table:
- Assigns a unique
location_id to each city to avoid redundancy.
- Maintains consistency in city names.
-
Book_Locations Table:
- Now references
location_id instead of raw city names, ensuring referential integrity.
-
Advantages:
- Avoids duplication of city names across multiple records.
- Simplifies updates and maintenance; changing a city name requires updating only one record in the
Locations table.
Summary of Normalized Tables
1. First Normal Form (1NF)
books_1NF
| title_id | title_name | retail_price | location |
|---|
| 102 | 200 Years of German Humor | 19.95 | New York |
| 102 | 200 Years of German Humor | 19.95 | San Francisco |
| 103 | Ask Your System Administrator | 39.95 | Chicago |
| 104 | But I Did It Unconsciously | 12.99 | Miami |
| 104 | But I Did It Unconsciously | 12.99 | London |
| 104 | But I Did It Unconsciously | 12.99 | New York |
2. Second Normal Form (2NF)
Books
| title_id | title_name | retail_price |
|---|
| 102 | 200 Years of German Humor | 19.95 |
| 103 | Ask Your System Administrator | 39.95 |
| 104 | But I Did It Unconsciously | 12.99 |
Book_Locations
| title_id | location |
|---|
| 102 | New York |
| 102 | San Francisco |
| 103 | Chicago |
| 104 | Miami |
| 104 | London |
| 104 | New York |
3. Third Normal Form (3NF)
Books
| title_id | title_name | retail_price |
|---|
| 102 | 200 Years of German Humor | 19.95 |
| 103 | Ask Your System Administrator | 39.95 |
| 104 | But I Did It Unconsciously | 12.99 |
Locations
| location_id | city_name |
|---|
| 1 | New York |
| 2 | San Francisco |
| 3 | Chicago |
| 4 | Miami |
| 5 | London |
Book_Locations
| title_id | location_id |
|---|
| 102 | 1 |
| 102 | 2 |
| 103 | 3 |
| 104 | 4 |
| 104 | 5 |
| 104 | 1 |
Benefits of Normalization
- Eliminates Redundancy: Reduces duplicate data, saving storage and maintaining consistency.
- Improves Data Integrity: Ensures that updates, insertions, and deletions are handled smoothly without anomalies.
- Enhances Query Efficiency: Simplifies queries by structuring data logically.
- Facilitates Maintenance: Easier to manage and update data structures.
By following these normalization steps, the database structure becomes more efficient, reliable, and easier to maintain.