A comprehensive, production-grade database project designed and optimized for a multi-seller E-commerce Marketplace using MySQL 8.0. This project demonstrates industry-standard database schema design, normalization, ACID transaction management, advanced analytics (Window Functions & CTEs), database programmability (Stored Procedures & Triggers), and query performance tuning.
- High-Fidelity Schema Design: Implement a fully normalized (3NF) relational schema that eliminates data redundancy.
- Strict Referential Integrity: Enforce marketplace-specific constraints using composite foreign keys.
- Advanced Querying: Demonstrate proficiency in complex analytical SQL (Window Functions, CTEs, Correlated Subqueries).
- Database Programmability: Encapsulate business logic directly within the database engine using Stored Procedures and Triggers.
- Query Performance Optimization: Analyze execution plans (
EXPLAIN) and design single-column and composite indexes to minimize query costs.
online-store-database/
├── README.md
├── schema/
│ └── 01_create_tables.sql # Schema definition, table constraints & indexes
└── sql/
├── 02_insert_data.sql # Rich seed data (consistent marketplace state)
├── 03_queries.sql # Basic SELECT, filtering, and aggregate queries
├── 04_join_queries.sql # Advanced multi-table JOINs (INNER, LEFT)
├── 05_subqueries.sql # Scalar, multi-row, and correlated subqueries
├── 06_conditional_queries.sql # Conditional logic (CASE & COALESCE)
├── 07_advanced_queries.sql # Complex analytics (CTEs & Window Functions)
├── 08_data_modification.sql # DML, ACID-compliant transactions & Views
├── 09_indexing_and_optimization.sql # Query performance tuning with EXPLAIN
└── 10_procedures_and_triggers.sql # Stored Procedures and BEFORE UPDATE Triggers
The database consists of 10 core relational tables designed around a modern Marketplace business model:
categories: Defines product taxonomy.customers: Holds user profiles.addresses: Handles user shipping addresses (1:N relation with customers).products: Acts as a global product catalog (decoupled from price/stock).sellers: Holds seller/merchant profiles.product_sellers: Bridge table (N:M relation between products and sellers). Acts as the Single Source of Truth (SSOT) for inventory stock and pricing per merchant.orders: Stores root order information.order_items: Line items for orders. Enforces a composite foreign key referencing(product_id, seller_id)fromproduct_sellersto guarantee order-to-merchant consistency.payments: Handles financial transactions (1:1 relation with orders).reviews: Stores product reviews (enforces aUNIQUE(customer_id, product_id)constraint to restrict users to one review per product).
- Stored Procedure (
GetProductOffers): A parameterized routine that accepts aproduct_idand returns all available merchant offers (sorted by price) where stock is greater than zero. - Database Trigger (
trg_prevent_negative_stock): A robustBEFORE UPDATEtrigger onproduct_sellersthat intercepts DML updates and raises a custom database exception viaSIGNAL SQLSTATE '45000'if the stock falls below zero.
- Implements atomic, multi-step transactions using
START TRANSACTIONandCOMMITto safely decrement merchant stock and insert pending orders concurrently, avoiding race conditions and ensuring data consistency.
- Analytical Windowing: Employs
ROW_NUMBER()andDENSE_RANK()for partitioning and ranking products within categories,SUM() OVERfor calculating rolling/cumulative revenue streams, andLAG()to analyze transaction-to-transaction financial differences. - Chained CTEs: Uses nested Common Table Expressions (
WITH ... AS) to perform complex multi-level aggregations, such as benchmarking individual customer spending against the platform average.
- Implements B-Tree indexes on heavily queried columns:
- Single-column index on
products(name)for fast string matching. - Composite index on
orders(customer_id, status)for optimizing customer dashboard queries. - Range index on
product_sellers(price)to accelerate sorting and range filtering.
- Single-column index on
- Utilizes the
EXPLAINkeyword to verify execution plan optimizations, transforming costly Full Table Scans (type: ALL) into extremely fast indexed scans (type: refortype: range).
products_overview_view: Consolidates product catalog statistics (categories, lowest available price, total stock across all merchants, and merchant counts) into a simplified virtual table, providing a clean API for frontend consumption.
- MySQL 8.0 installed locally or running in a container.
- Access to a MySQL command-line client or terminal.
- Clone the repository:
git clone https://github.com/May-tea/online-store-database.git
cd online-store-database- Initialize the database schema and constraints:
mysql -u devuser -p < schema/01_create_tables.sql- Seed the database with high-integrity sample data:
mysql -u devuser -p < sql/02_insert_data.sql- You can now execute and explore the query files located in the
sql/directory sequentially (from03to10) using your preferred database client or direct terminal queries.
- Mahdiyar Babaghassabha
- GitHub: @May-tea