Designing the database schema for a new e-commerce platform and considering factors like normalization, denormalization, indexing, and performance optimization to ensure efficient storage and retrieval of data.

3 min readJun 8, 2024

Designing the database schema for a new e-commerce platform involves careful consideration of various factors to ensure efficient storage and retrieval of data. Here’s a structured approach

1. Requirements Analysis

Understand the requirements:

  • User management
  • Product catalog
  • Inventory management
  • Orders and transactions
  • Shopping cart
  • Reviews and ratings
  • Payment processing

2. Entity-Relationship (ER) Diagram

Create an ER diagram to identify entities and relationships. Common entities include:

  • Users
  • Products
  • Categories
  • Orders
  • OrderItems
  • CartItems
  • Reviews
  • Payments

3. Normalization

Normalize the database to eliminate redundancy and ensure data integrity. Typically, aim for at least Third Normal Form (3NF).

4. Schema Design

Users Table

CREATE TABLE Users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
first_name VARCHAR(50),
last_name VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Products Table

CREATE TABLE Products (
product_id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL,
stock_quantity INT NOT NULL,
category_id INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (category_id) REFERENCES Categories(category_id)
);

Categories Table

CREATE TABLE Categories (
category_id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL
);

Orders Table

CREATE TABLE Orders (
order_id SERIAL PRIMARY KEY,
user_id INT NOT NULL,
total_amount DECIMAL(10, 2) NOT NULL,
status VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES Users(user_id)
);

OrderItems Table

CREATE TABLE OrderItems (
order_item_id SERIAL PRIMARY KEY,
order_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
price DECIMAL(10, 2) NOT NULL,
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
FOREIGN KEY (product_id) REFERENCES Products(product_id)
);

CartItems Table

CREATE TABLE CartItems (
cart_item_id SERIAL PRIMARY KEY,
user_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES Users(user_id),
FOREIGN KEY (product_id) REFERENCES Products(product_id)
);

Reviews Table

CREATE TABLE Reviews (
review_id SERIAL PRIMARY KEY,
user_id INT NOT NULL,
product_id INT NOT NULL,
rating INT CHECK (rating >= 1 AND rating <= 5),
comment TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES Users(user_id),
FOREIGN KEY (product_id) REFERENCES Products(product_id)
);

Payments Table

CREATE TABLE Payments (
payment_id SERIAL PRIMARY KEY,
order_id INT NOT NULL,
payment_method VARCHAR(50),
amount DECIMAL(10, 2) NOT NULL,
status VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (order_id) REFERENCES Orders(order_id)
);

5. Denormalization

Consider denormalization for read-heavy operations. For example, storing the total amount in the Orders table instead of calculating it from OrderItems every time.

6. Indexing

Create indexes to speed up query performance. Common indexes include:

  • Primary keys (automatically indexed)
  • Foreign keys
  • Columns frequently used in WHERE clauses or JOIN conditions
CREATE INDEX idx_users_email ON Users(email);
CREATE INDEX idx_products_category_id ON Products(category_id);
CREATE INDEX idx_orders_user_id ON Orders(user_id);

7. Performance Optimization

Partitioning: Consider table partitioning for large tables to improve query performance. Materialized Views: Use materialized views for complex queries that do not change frequently. Caching: Implement caching for frequently accessed data to reduce database load.

8. Transactions and Concurrency

Ensure transactions are properly managed to maintain data integrity, especially for critical operations like order processing. Use appropriate isolation levels to handle concurrency.

9. Backup and Recovery

Design a robust backup and recovery strategy to prevent data loss.

10. Monitoring and Maintenance

Set up monitoring for database performance and regularly maintain the database (e.g., reindexing, vacuuming).

Example ER Diagram Overview

+-----------------+         +-----------------+         +----------------+
| Users | | Categories | | Products |
+-----------------+ +-----------------+ +----------------+
| user_id (PK) | 1 N | category_id (PK)| 1 N | product_id (PK)|
| username |---------| name |---------| name |
| email | +-----------------+ | description |
| password_hash | | price |
| ... | | stock_quantity |
+-----------------+ | category_id (FK)|
+----------------+
| 1 +-------------+
+-----------| Orders |
+-------------+
| order_id (PK)|
| user_id (FK) |
| total_amount |
| status |
| ... |
+-------------+
|
| 1
|
N
+-------------+
| OrderItems |
+-------------+
| order_item_id(PK)|
| order_id (FK) |
| product_id (FK) |
| quantity |
| price |
+-------------------+

By following these steps and considering various optimization techniques, you can design an e-commerce database schema that balances data integrity, efficient storage, and fast retrieval of information for a smooth user experience. Remember, the optimal schema will depend on the specific needs and functionalities of your e-commerce platform.

--

--

Biswanath Giri
Biswanath Giri

Written by Biswanath Giri

Cloud & AI Architect | Empowering People in Cloud Computing, Google Cloud AI/ML, and Google Workspace | Enabling Businesses on Their Cloud Journey

Responses (1)