Certainly! I'd be happy to guide you through the process of normalizing a relation from 1NF to higher normal forms using an example unrelated to schools or universities. Let's consider a practical example in the context of an online marketplace.
Example Domain: An online marketplace that connects artisans with buyers. The marketplace tracks information about orders, artisans, and products.
1. Attributes of the Initial Relation
Let's define a relation OrderDetails with the following attributes:
- OrderID: Unique identifier for each order.
- BuyerID: Unique identifier for each buyer.
- BuyerName: Name of the buyer.
- ArtisanID: Unique identifier for each artisan.
- ArtisanName: Name of the artisan.
- ProductID: Unique identifier for each product.
- ProductName: Name of the product.
- ProductCategory: Category of the product.
- OrderDate: Date when the order was placed.
- Quantity: Quantity of the product ordered.
- UnitPrice: Price per unit of the product.
- TotalPrice: Total price for the order line (Quantity * UnitPrice).
2. Non-Trivial and Left-Irreducible Functional Dependencies
Based on the business rules of the marketplace, the following functional dependencies (FDs) exist:
- OrderID → BuyerID, OrderDate
- BuyerID → BuyerName
- ArtisanID → ArtisanName
- ProductID → ProductName, ProductCategory, UnitPrice, ArtisanID
- {OrderID, ProductID} → Quantity, TotalPrice
These dependencies are non-trivial and left-irreducible (the left side cannot be reduced further without losing the dependency).
3. Potential Keys of the Initial Relation
Potential keys (candidate keys) are minimal superkeys that can uniquely identify a tuple in the relation.
- Primary Key: {OrderID, ProductID}
This combination uniquely identifies each record in the OrderDetails relation because an order can contain multiple products.
4. Determinants of the Functional Dependencies
- OrderID is the determinant for BuyerID and OrderDate.
- BuyerID is the determinant for BuyerName.
- ArtisanID is the determinant for ArtisanName.
- ProductID is the determinant for ProductName, ProductCategory, UnitPrice, ArtisanID.
- {OrderID, ProductID} is the determinant for Quantity and TotalPrice.
5. Normal Forms Analysis
First Normal Form (1NF)
A relation is in 1NF if all the attributes are atomic (indivisible).
- Analysis: The OrderDetails relation is in 1NF because all attributes contain atomic, indivisible values.
Second Normal Form (2NF)
A relation is in 2NF if it is in 1NF and every non-prime attribute is fully functionally dependent on the whole of every candidate key.
- Analysis:
- Non-prime attributes: All attributes except those in the candidate key ({OrderID, ProductID}).
- Partial Dependencies:
- OrderID → BuyerID, OrderDate (Partial dependency on part of the candidate key)
- ProductID → ProductName, ProductCategory, UnitPrice, ArtisanID (Partial dependency)
- Conclusion: The relation is not in 2NF because there are attributes dependent on parts of the primary key, leading to partial dependencies.
Third Normal Form (3NF)
A relation is in 3NF if it is in 2NF, and all the attributes are functionally dependent only on the primary key and not on any other non-prime attributes (no transitive dependencies).
- Analysis: Since the relation is not in 2NF, it cannot be in 3NF.
Boyce-Codd Normal Form (BCNF)
A relation is in BCNF if it is in 3NF and for every one of its dependencies X → Y, X is a superkey.
- Analysis: Since the relation is not in 3NF, it cannot be in BCNF.
Fourth Normal Form (4NF) and Fifth Normal Form (5NF)
These higher normal forms deal with multi-valued dependencies and join dependencies, respectively.
- Analysis: Not applicable until the relation is at least in BCNF.
6. Anomalies in the Initial Relation
Insertion Anomaly
- Example: Adding a new product that hasn't been ordered yet requires an OrderID, forcing the insertion of a dummy order record.
Deletion Anomaly
- Example: Deleting the last order of a buyer also removes the buyer's information from the database.
Update Anomaly
- Example: Changing the price of a product requires updating multiple records; if one is missed, data inconsistency occurs.
Causes of Anomalies
- Partial Dependencies: Attributes depend on part of the composite primary key.
- Redundant Data: Repetition of buyer, artisan, and product information across multiple records.
7. Decomposition of the Initial Relation
To eliminate anomalies, we decompose the relation into smaller relations that satisfy higher normal forms.
a. Relation: Orders
Attributes:
- OrderID (Primary Key)
- BuyerID
- OrderDate
Functional Dependencies:
- OrderID → BuyerID, OrderDate
b. Relation: Buyers
Attributes:
- BuyerID (Primary Key)
- BuyerName
Functional Dependencies:
c. Relation: Products
Attributes:
- ProductID (Primary Key)
- ProductName
- ProductCategory
- UnitPrice
- ArtisanID
Functional Dependencies:
- ProductID → ProductName, ProductCategory, UnitPrice, ArtisanID
d. Relation: Artisans
Attributes:
- ArtisanID (Primary Key)
- ArtisanName
Functional Dependencies:
e. Relation: OrderLineItems
Attributes:
- OrderID (Foreign Key)
- ProductID (Foreign Key)
- Quantity
- TotalPrice
Primary Key: {OrderID, ProductID}
Functional Dependencies:
- {OrderID, ProductID} → Quantity, TotalPrice
8. Normal Forms of Relations After Decomposition
All decomposed relations are in 3NF and BCNF because:
- All non-key attributes are fully functionally dependent on the primary key.
- There are no partial or transitive dependencies.
9. Eliminating Anomalies
Insertion Anomaly Resolved
- New products, buyers, or artisans can be added independently without requiring dummy orders.
Deletion Anomaly Resolved
- Deleting an order does not remove buyer or product information from the database.
Update Anomaly Resolved
- Updating product prices or buyer information is done in a single place, ensuring data consistency.
10. Conclusion
By decomposing the initial relation into multiple relations that satisfy higher normal forms, we eliminate redundancy and resolve insertion, deletion, and update anomalies. This normalization ensures data integrity and efficient database operations.
This example illustrates the process of analyzing a relation for normalization, identifying functional dependencies, and performing decomposition to achieve higher normal forms. You can apply this methodology to any domain by identifying the entities, attributes, and business rules specific to that domain.