Lab 20: Capstone — E-Commerce Database

Time: 60 minutes | Level: Foundations | DB: MySQL 8 / PostgreSQL 15

Overview

Design and build a complete, normalized e-commerce database from scratch. Create 7 tables with full FK constraints, load 50+ rows of realistic sample data, write complex reporting queries, and add performance indexes.


Step 1: Start the Database

docker run -d --name mysql-lab -e MYSQL_ROOT_PASSWORD=rootpass mysql:8.0
for i in $(seq 1 30); do docker exec mysql-lab mysql -uroot -prootpass -e "SELECT 1" 2>/dev/null && break || sleep 2; done

Step 2: Schema Design

Our e-commerce schema implements 3NF normalization with 7 tables:

categories        ← product category hierarchy
products          ← product catalog (FK: categories)
users             ← customer accounts
addresses         ← shipping addresses (FK: users)
orders            ← purchase orders (FK: users, addresses)
order_items       ← line items per order (FK: orders, products)
reviews           ← product reviews (FK: users, products)

Entity Relationship:


Step 3: Create the Schema

📸 Verified Output:


Step 4: Insert Sample Data — Categories and Products


Step 5: Insert Users, Addresses, and Orders


Step 6: Insert Reviews


Step 7: Complex Reporting Queries

📸 Verified Output (Top Products by Revenue):

📸 Verified Output:


Step 8: Capstone Verification — Full Schema Health Check

📸 Verified Output (row counts):

📸 Verified Output (monthly revenue):

Cleanup:


Summary — What You Built

Table
Rows
Purpose

categories

14

Hierarchical product taxonomy

products

20

Full product catalog with cost/price

users

10

Customer accounts

addresses

11

Multi-address per user

orders

15

Purchase orders with status lifecycle

order_items

23

Line items with generated line_total

reviews

20

Verified purchase reviews

Total

113

Complete e-commerce dataset

Design Principles Applied:

  • ✅ 3NF normalization throughout

  • ✅ Foreign keys with appropriate ON DELETE behavior

  • ✅ CHECK constraints on critical fields

  • ✅ GENERATED column for line_total

  • ✅ Composite UNIQUE keys (one review per user per product)

  • ✅ Performance indexes on FK and WHERE columns

  • ✅ Price snapshot in order_items (historical accuracy)

  • ✅ ENUM for order status lifecycle

Queries Demonstrated:

  • Top products by revenue (JOIN + GROUP BY + ORDER)

  • Customer lifetime value (LEFT JOIN + conditional aggregation)

  • Inventory report (multi-table JOIN + subquery)

  • Monthly revenue trend (DATE_FORMAT + GROUP BY)

  • Schema health check (UNION ALL row counts)

Congratulations — you have completed all 20 Database Foundations labs! 🎉


Complete Course Summary

Labs
Topic Area

01-02

Setup, relational model, CREATE

03-05

DML basics: INSERT, SELECT, aggregates

06-08

JOINs and subqueries

09-10

Views and normalization

11-12

Constraints and indexes

13-14

UPDATE/DELETE and transactions

15-17

Functions: string, date, NULL

18-19

DB-specific: PostgreSQL JSONB, MySQL features

20

Capstone: full e-commerce schema

Last updated