Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views2 pages

Ecommerce DB Structure

The document outlines the SQL schema for an e-commerce database, including tables for users, categories, brands, products, shopping carts, orders, order items, payments, and reviews. It defines relationships such as one-to-many between users and orders, and many-to-one between products and categories. Additionally, it specifies foreign key constraints to maintain data integrity across the tables.

Uploaded by

sa4381865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Ecommerce DB Structure

The document outlines the SQL schema for an e-commerce database, including tables for users, categories, brands, products, shopping carts, orders, order items, payments, and reviews. It defines relationships such as one-to-many between users and orders, and many-to-one between products and categories. Additionally, it specifies foreign key constraints to maintain data integrity across the tables.

Uploaded by

sa4381865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

CREATE TABLE users (

id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,


role,
phone ,
);

CREATE TABLE categories (


id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name,
slug,
description,
);

CREATE TABLE brands (


id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name,
);

CREATE TABLE products (


id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name,
description,
price,
stock,
color,
category_id,
brand_id,
sku,
image,
);

CREATE TABLE shopping_cart (


id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT UNSIGNED,
product_id BIGINT UNSIGNED,
quantity INT,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
);

CREATE TABLE orders (


id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id,
total_price,
shipping_address,
order_status ENUM('pending', 'shipped', 'delivered', 'canceled'),
payment_status ENUM('unpaid', 'paid'),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE order_items (


id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
order_id BIGINT UNSIGNED,
product_id BIGINT UNSIGNED,
quantity,
unit_price,
FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
);

CREATE TABLE payments (


id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
order_id BIGINT UNSIGNED,
payment_method ENUM('credit_card', 'paypal', 'bank_transfer'),
payment_status ENUM('pending', 'completed', 'failed'),
amount DECIMAL(10, 2),
transaction_id VARCHAR(255),
FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE
);

CREATE TABLE reviews (


id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
product_id BIGINT UNSIGNED,
user_id BIGINT UNSIGNED,
rating INT CHECK (rating >= 1 AND rating <= 5),
review_text TEXT,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

Relationships Overview:
Users & Orders: One-to-Many (One user can place many orders).
Orders & Order Items: One-to-Many (Each order can have multiple order items).
Products & Order Items: Many-to-One (Many order items can reference a product).
Products & Categories: Many-to-One (A product belongs to one category).
Products & Brands: Many-to-One (A product belongs to one brand).
Users & Reviews: One-to-Many (A user can leave multiple reviews).
Products & Reviews: One-to-Many (A product can have multiple reviews).

You might also like