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).