CHAPTER 29
Beginner
Database Design Project (E-Commerce)
Updated: May 16, 2026
7 min read
# CHAPTER 29
Database Design Project (E-Commerce)
1. Introduction
You have learned the theory. You have mastered the syntax. Now, you must act as the Lead Database Architect. A client wants to build an E-Commerce platform (like Amazon or Shopify). If you design the database poorly, the company will face data corruption, impossible reporting, and catastrophic performance issues. In this chapter, we synthesize everything we've learned to design a fully normalized, production-grade E-Commerce schema.2. Learning Objectives
By the end of this project, you will be able to:- Identify the core entities required for an E-Commerce system.
- Apply Database Normalization rules to prevent redundancy.
- Map One-to-Many and Many-to-Many relationships.
-
Write the final
CREATE TABLEscripts with full constraints.
3. Step 1: Identifying the Entities
Before writing any SQL, we grab a whiteboard and list the core "Nouns" (Entities) of our business:- 1. Users: The customers making purchases.
- 2. Products: The items being sold.
- 3. Categories: The groupings of products (e.g., Electronics, Clothing).
- 4. Orders: The master record of a purchase.
- 5. Order_Items: The specific products attached to a specific order.
4. Step 2: Mapping the Relationships
How do these entities mathematically connect?- A Category has Many Products. A Product belongs to One Category. (1:N)
- A User has Many Orders. An Order belongs to One User. (1:N)
-
An Order has Many Products. A Product can be in Many Orders. (N:M) -> *We must use the
order_itemsPivot Table!*
5. Step 3: Writing the DDL (Data Definition Language)
We will write the tables in the exact order necessary to satisfy Foreign Key constraints. (You must create the Parent before you can create the Child!).#### The Parents (No Foreign Keys)
sql
#### The Children (1:N Relationships)
sql
#### The Pivot Table (N:M Relationship)
sql
6. Architectural Brilliance: price_at_purchase
Why did we put a price_at_purchase column inside the order_items table? Can't we just JOIN to the products table to see the price?
NO!
If John buys a Laptop today for $1,000, and tomorrow the Admin raises the price to $1,200... if John looks at his receipt history, it will say he paid $1,200! This violates accounting laws.
By explicitly saving the price_at_purchase in the pivot table *at the exact moment of the transaction*, we create an immutable financial record that survives future price changes.
7. Step 4: The Ultimate Reporting Query
Let's prove our architecture works. The CEO asks: *"Give me a list of all shipped orders over $500, including the customer's email and the names of the products they bought."*
sql
*Because our Foreign Keys are perfectly mapped, this 4-table join executes flawlessly.*
8. Common Mistakes
-
Storing calculated totals blindly: While we store
total_amountin theorderstable for caching/speed, you must ensure that your backend PHP code strictly calculates that total by summing up theorder_items. Never trust a total passed directly from the frontend HTML cart!
9. Best Practices
-
Indexes for Speed: In production, you would immediately add an index to
user_idin theorderstable, because the most common query on the website will be: "Show me My Orders."
10. Exercises
-
1.
Analyze the schema: If a database admin attempts to
DELETEa category that currently has 50 products assigned to it, what will MySQL do?
- 2. Write the SQL query to find the total sum of revenue generated by all orders that are currently marked as 'Delivered'.
11. MCQ Quiz with Answers
Question 1
In the E-Commerce schema provided, why is the order_items table strictly necessary?
Question 2
Why is it a critical architectural best practice to store the price_at_purchase inside the order_items table, rather than just referencing the main products table?
12. Interview Questions
- Q: Walk me through your database design for a basic E-Commerce cart system. Detail the relationships between Users, Orders, and Products, and specifically explain how you track historical pricing.
-
Q: Look at the
ON DELETE CASCADErule applied to theorder_idin theorder_itemstable. Explain what this does and why it was not applied to theproduct_idconstraint.
13. FAQs
Q: How do we handle shopping carts before the user checks out? A: You can either store active Carts in a temporary NoSQL database (like Redis) for blistering speed, or create acarts and cart_items table in MySQL that perfectly mirrors the structure of the Orders tables!