This example shows how to fix partial dependency by creating separate tables for products and order details, linking them with keys.
-- Original table with partial dependency
CREATE TABLE Orders (
OrderID INT,
ProductID INT,
ProductName VARCHAR(50),
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);
-- Insert sample data
INSERT INTO Orders VALUES (1, 101, 'Pen', 10);
INSERT INTO Orders VALUES (1, 102, 'Notebook', 5);
-- Normalize to 2NF by splitting tables
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50)
);
CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
-- Insert data into new tables
INSERT INTO Products VALUES (101, 'Pen');
INSERT INTO Products VALUES (102, 'Notebook');
INSERT INTO OrderDetails VALUES (1, 101, 10);
INSERT INTO OrderDetails VALUES (1, 102, 5);
-- Query to show normalized data
SELECT od.OrderID, od.ProductID, p.ProductName, od.Quantity
FROM OrderDetails od
JOIN Products p ON od.ProductID = p.ProductID
ORDER BY od.OrderID, od.ProductID;