Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 120

Online Shopping Database Project in MySQL

In this lesson: E-commerce database project in MySQL — customers, products, orders tables, keys and sales queries with output. In Hindi & English.

The project

An online shopping (e-commerce) database is a rich DBMS project. It models customers, products, orders, and the items in each order. It shows both a one-to-many relationship (customer to orders) and a many-to-many one (orders to products), resolved by an order items table.

💡 customer → orders is one-to-many; orders ↔ products is many-to-many via order_items.

Designing the tables

  • customers — id, name, city.
  • products — id, name, price.
  • orders — id, customer_id (FK), order_date.
  • order_items — id, order_id (FK), product_id (FK), quantity, price.

Each order belongs to one customer. Each order can have many products through order_items.

Creating the schema

CREATE TABLE customers (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL, city VARCHAR(40)
);
CREATE TABLE products (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(80) NOT NULL, price DECIMAL(10,2)
);
CREATE TABLE orders (
    id INT PRIMARY KEY AUTO_INCREMENT,
    customer_id INT, order_date DATE,
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);
CREATE TABLE order_items (
    id INT PRIMARY KEY AUTO_INCREMENT,
    order_id INT, product_id INT,
    quantity INT, price DECIMAL(10,2),
    FOREIGN KEY (order_id) REFERENCES orders(id),
    FOREIGN KEY (product_id) REFERENCES products(id)
);

The order items link

order_items is the many-to-many link between orders and products. Each row is one product line in an order, with its quantity and the price at purchase time. Storing the price here keeps old order totals accurate even if the product price changes later.

Sample data

INSERT INTO customers (name, city) VALUES ('Aman', 'Delhi'), ('Diya', 'Mumbai');
INSERT INTO products (name, price) VALUES ('Pen', 10.00), ('Notebook', 50.00);
INSERT INTO orders (customer_id, order_date) VALUES (1, '2026-09-20');
INSERT INTO order_items (order_id, product_id, quantity, price) VALUES
    (1, 1, 3, 10.00), (1, 2, 2, 50.00);

Sales queries

-- total for one order
SELECT SUM(quantity * price) AS order_total
FROM order_items WHERE order_id = 1;

-- best-selling products
SELECT p.name, SUM(oi.quantity) AS sold
FROM order_items oi JOIN products p ON oi.product_id = p.id
GROUP BY p.id ORDER BY sold DESC;

-- total spending per customer
SELECT c.name, SUM(oi.quantity * oi.price) AS spent
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
GROUP BY c.id;
-- order 1 total: 130.00 (3×10 + 2×50)

Common mistakes

  • Putting products directly in orders. Use order_items for many products per order.
  • Not storing price in order_items. Then old totals change when a product's price does.
  • Forgetting quantity. An order line needs how many were bought.
  • No foreign keys. Link items to real orders and products.

Summary

  • An e-commerce database has customers, products, orders and order_items.
  • customer → orders is one-to-many; orders ↔ products is many-to-many.
  • order_items stores each product line with quantity and price.
  • Order total = SUM(quantity × price) over its items.
  • Query best-sellers and spending with JOIN and GROUP BY.

Next, revise many-to-many in ER diagram examples, or build the library database project.

Frequently Asked Questions

How do I design an online shopping database in MySQL?
Create customers, products, orders and order_items tables. An order belongs to one customer (one-to-many), and order_items links orders and products (many-to-many), storing quantity and price for each line of an order.
Why do I need an order_items table?
An order can contain many products, and a product can appear in many orders — a many-to-many relationship. The order_items link table records each product in each order, with its quantity and price, which a single foreign key cannot do.
How do I calculate an order total in MySQL?
Sum quantity times price across the order_items for that order: SELECT SUM(quantity * price) FROM order_items WHERE order_id = ?;. Storing the price on each line keeps totals correct even if the product price later changes.
How do I find the best-selling products?
Group order_items by product and sum the quantity: SELECT product_id, SUM(quantity) FROM order_items GROUP BY product_id ORDER BY SUM(quantity) DESC;. Join to products to show names.
Why store the price in order_items and not just in products?
Product prices change over time. Storing the price at the moment of purchase in order_items means old orders keep their correct historical totals, even after the product price is updated.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicStudent Marksheet Database Project Your progress: 120 / 151 (79%) · Lesson 121 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free