Below is an example walkthrough. Suppose you originally have two “fat” tables that mix information about orders, customers, products, and even payments. For example, imagine your un‐normalized tables look something like this:
Un‑Normalized Tables
Table 1: SalesOrders_UnNorm
(Each row repeats customer, product, and payment information for an order.)
CREATE TABLE SalesOrders_UnNorm (
OrderID INT,
OrderDate DATE,
CustomerName VARCHAR(50),
CustomerAddress VARCHAR(100),
CustomerPhone VARCHAR(20),
ProductID INT,
ProductName VARCHAR(50),
ProductPrice DECIMAL(10,2),
Quantity INT,
PaymentMethod VARCHAR(20),
PaymentDate DATE,
PaymentAmount DECIMAL(10,2)
);
Sample Insert into SalesOrders_UnNorm
INSERT INTO SalesOrders_UnNorm VALUES
(101, '2023-10-01', 'Alice Smith', '123 Oak St', '555-1111', 1001, 'Widget A', 9.99, 3, 'Credit Card', '2023-10-02', 29.97),
(101, '2023-10-01', 'Alice Smith', '123 Oak St', '555-1111', 1002, 'Widget B', 14.99, 2, 'Credit Card', '2023-10-02', 29.98),
(102, '2023-10-03', 'Bob Jones', '456 Maple Ave', '555-2222', 1001, 'Widget A', 9.99, 1, 'Cash', '2023-10-03', 9.99);
Table 2: OrderComments_UnNorm (an extra table with redundant order-level data)
(For simplicity, assume each row contains the order’s ID and a comment, but it repeats order info.)
CREATE TABLE OrderComments_UnNorm (
OrderID INT,
CommentText VARCHAR(255),
CommentDate DATE
);
Sample Insert into OrderComments_UnNorm
INSERT INTO OrderComments_UnNorm VALUES
(101, 'Rush delivery requested', '2023-10-01'),
(102, 'Call customer before delivery', '2023-10-03');
Normalization Goals
We want to remove redundancy and achieve (at least) Third Normal Form (3NF). In our un‑normalized design:
- Customer details (name, address, phone) are repeated on every row for an order.
- Product details (name, price) are repeated with every order item.
- Payment details are attached redundantly to each product row.
- Even the OrderComments table repeats order identification that could be linked through the normalized orders.
Following 3NF (and even 4NF if needed for multi‑valued facts) we break the design into several tables, each representing one “entity” with its attributes and properly set foreign keys.
Normalized Schema (3NF Design)
We can decompose the original design into these tables:
- Customers – Contains customer information.
- Orders – Contains header information about an order, linking to a customer.
- Products – Contains product information.
- OrderItems – Holds each order line with a product reference and quantity.
- Payments – Contains payment information for orders.
- OrderComments – Contains order comments (linked to an OrderID).
Below are the normalized DDL statements and some sample DML.
1. Customers Table
CREATE TABLE Customers (
CustomerID INT IDENTITY(1,1) PRIMARY KEY,
CustomerName VARCHAR(50) NOT NULL,
CustomerAddress VARCHAR(100),
CustomerPhone VARCHAR(20)
);
Sample Data:
INSERT INTO Customers (CustomerName, CustomerAddress, CustomerPhone)
VALUES
('Alice Smith', '123 Oak St', '555-1111'),
('Bob Jones', '456 Maple Ave', '555-2222');
2. Orders Table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
OrderDate DATE NOT NULL,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
Sample Data:
(Assuming OrderID 101 belongs to Alice and 102 to Bob – you must lookup the CustomerID accordingly.)
-- Suppose Alice got CustomerID = 1 and Bob got CustomerID = 2
INSERT INTO Orders (OrderID, OrderDate, CustomerID)
VALUES
(101, '2023-10-01', 1),
(102, '2023-10-03', 2);
3. Products Table
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50) NOT NULL,
ProductPrice DECIMAL(10,2) NOT NULL
);
Sample Data:
INSERT INTO Products (ProductID, ProductName, ProductPrice)
VALUES
(1001, 'Widget A', 9.99),
(1002, 'Widget B', 14.99);
4. OrderItems Table
Each order may contain one or more products.
CREATE TABLE OrderItems (
OrderItemID INT IDENTITY(1,1) PRIMARY KEY,
OrderID INT NOT NULL,
ProductID INT NOT NULL,
Quantity INT NOT NULL,
-- Optionally, you can compute Total as Payment is Quantity * ProductPrice
-- or store it if needed.
Total AS (Quantity * (SELECT ProductPrice FROM Products WHERE Products.ProductID = OrderItems.ProductID)),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
Sample Data:
INSERT INTO OrderItems (OrderID, ProductID, Quantity)
VALUES
(101, 1001, 3),
(101, 1002, 2),
(102, 1001, 1);
Note: The computed column Total is optional. In some designs you might store the total, although storing derived data is generally avoided unless performance is a concern.
5. Payments Table
If an order can have one or more payments, keep this separate.
CREATE TABLE Payments (
PaymentID INT IDENTITY(1,1) PRIMARY KEY,
OrderID INT NOT NULL,
PaymentMethod VARCHAR(20),
PaymentDate DATE,
PaymentAmount DECIMAL(10,2),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);
Sample Data:
Since in our un‑normalized table the payment info was repeated per product in order 101 and 102, we assume one payment per order in this sample:
INSERT INTO Payments (OrderID, PaymentMethod, PaymentDate, PaymentAmount)
VALUES
(101, 'Credit Card', '2023-10-02', 29.97 + 29.98), -- Sum of order 101 items
(102, 'Cash', '2023-10-03', 9.99);
Tip: In practice the payment amount would be calculated from the order totals (or vice versa, you’d handle that in your application).
6. OrderComments Table
We preserve the comments (linked by OrderID):
CREATE TABLE OrderComments (
CommentID INT IDENTITY(1,1) PRIMARY KEY,
OrderID INT NOT NULL,
CommentText VARCHAR(255),
CommentDate DATE,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);
Sample Data:
INSERT INTO OrderComments (OrderID, CommentText, CommentDate)
VALUES
(101, 'Rush delivery requested', '2023-10-01'),
(102, 'Call customer before delivery', '2023-10-03');
Why This Design Is in 3NF (and Beyond)
-
Elimination of Repetition:
Customer, product, and payment details are stored once in their own tables rather than repeating in each order row.
-
No Partial Dependencies:
In the Orders table, non‑key columns (e.g., OrderDate) depend only on the candidate key (OrderID). In OrderItems, Quantity depends on the combination (or surrogate key) of an individual order item.
-
No Transitive Dependencies:
For instance, in our design, Customer details are managed entirely within the Customers table—changing a customer’s address does not require changes in every order.
-
Multivalued Attributes:
In case an order can have multiple payments or comments, 4NF principles suggest keeping these in separate relations. Our separate Payments and OrderComments tables achieve that if an order can have several payments or comments.
This design minimizes update anomalies and keeps the data integrity high.
Conclusion
We took the original un‑normalized design with redundant columns in two tables and normalized it into six clean relational tables. This 3NF (and conceptually 4NF for multivalued facts) design is more efficient from a data‑integrity and maintainability point of view.
Feel free to adjust the design further based on any specific business rules (for example, if an order can have multiple payment splits or if the “Total” should be stored rather than computed).