345 lines
9.7 KiB
SQL
345 lines
9.7 KiB
SQL
-- E-Commerce Database Schema
|
|
-- Run this file to create tables and populate initial data
|
|
-- psql -U ecommerce_user -d ecommerce_db -h localhost -f schema.sql
|
|
|
|
-- ============================================
|
|
-- CREATE TABLES
|
|
-- ============================================
|
|
|
|
-- Category Table
|
|
CREATE TABLE category (
|
|
id SERIAL PRIMARY KEY,
|
|
name VARCHAR(100) UNIQUE NOT NULL,
|
|
slug VARCHAR(100) UNIQUE NOT NULL,
|
|
description TEXT
|
|
);
|
|
|
|
-- User Table
|
|
CREATE TABLE "user" (
|
|
id SERIAL PRIMARY KEY,
|
|
email VARCHAR(255) UNIQUE NOT NULL,
|
|
hashed_password VARCHAR(255) NOT NULL,
|
|
full_name VARCHAR(255),
|
|
phone VARCHAR(20),
|
|
address TEXT,
|
|
city VARCHAR(100),
|
|
postal_code VARCHAR(20),
|
|
country VARCHAR(100),
|
|
is_active BOOLEAN DEFAULT TRUE,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- Product Table
|
|
CREATE TABLE product (
|
|
id SERIAL PRIMARY KEY,
|
|
name VARCHAR(255) NOT NULL,
|
|
description TEXT,
|
|
price DECIMAL(10, 2) NOT NULL,
|
|
discount_price DECIMAL(10, 2),
|
|
category_id INTEGER NOT NULL,
|
|
gender VARCHAR(50),
|
|
brand VARCHAR(100),
|
|
sizes JSONB,
|
|
colors JSONB,
|
|
stock INTEGER DEFAULT 0,
|
|
images JSONB,
|
|
is_featured BOOLEAN DEFAULT FALSE,
|
|
is_on_sale BOOLEAN DEFAULT FALSE,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (category_id) REFERENCES category(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Cart Table
|
|
CREATE TABLE cart (
|
|
id SERIAL PRIMARY KEY,
|
|
user_id INTEGER UNIQUE NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Cart Item Table
|
|
CREATE TABLE cart_item (
|
|
id SERIAL PRIMARY KEY,
|
|
cart_id INTEGER NOT NULL,
|
|
product_id INTEGER NOT NULL,
|
|
quantity INTEGER DEFAULT 1,
|
|
size VARCHAR(50),
|
|
color VARCHAR(50),
|
|
FOREIGN KEY (cart_id) REFERENCES cart(id) ON DELETE CASCADE,
|
|
FOREIGN KEY (product_id) REFERENCES product(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Order Table
|
|
CREATE TABLE "order" (
|
|
id SERIAL PRIMARY KEY,
|
|
user_id INTEGER NOT NULL,
|
|
order_number VARCHAR(100) UNIQUE NOT NULL,
|
|
status VARCHAR(50) DEFAULT 'pending',
|
|
total_amount DECIMAL(10, 2) NOT NULL,
|
|
shipping_address TEXT,
|
|
shipping_city VARCHAR(100),
|
|
shipping_postal_code VARCHAR(20),
|
|
shipping_country VARCHAR(100),
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Order Item Table
|
|
CREATE TABLE order_item (
|
|
id SERIAL PRIMARY KEY,
|
|
order_id INTEGER NOT NULL,
|
|
product_id INTEGER NOT NULL,
|
|
quantity INTEGER NOT NULL,
|
|
price DECIMAL(10, 2) NOT NULL,
|
|
size VARCHAR(50),
|
|
color VARCHAR(50),
|
|
FOREIGN KEY (order_id) REFERENCES "order"(id) ON DELETE CASCADE,
|
|
FOREIGN KEY (product_id) REFERENCES product(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Wishlist (User-Product Association)
|
|
CREATE TABLE user_wishlist (
|
|
user_id INTEGER NOT NULL,
|
|
product_id INTEGER NOT NULL,
|
|
PRIMARY KEY (user_id, product_id),
|
|
FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE,
|
|
FOREIGN KEY (product_id) REFERENCES product(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Contact Message Table
|
|
CREATE TABLE contact_message (
|
|
id SERIAL PRIMARY KEY,
|
|
name VARCHAR(255),
|
|
email VARCHAR(255),
|
|
subject VARCHAR(255),
|
|
message TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- ============================================
|
|
-- CREATE INDEXES
|
|
-- ============================================
|
|
CREATE INDEX idx_user_email ON "user"(email);
|
|
CREATE INDEX idx_product_name ON product(name);
|
|
CREATE INDEX idx_product_category ON product(category_id);
|
|
CREATE INDEX idx_product_is_featured ON product(is_featured);
|
|
CREATE INDEX idx_product_is_on_sale ON product(is_on_sale);
|
|
CREATE INDEX idx_order_user ON "order"(user_id);
|
|
CREATE INDEX idx_order_created_at ON "order"(created_at);
|
|
CREATE INDEX idx_cart_item_cart ON cart_item(cart_id);
|
|
CREATE INDEX idx_order_item_order ON order_item(order_id);
|
|
|
|
-- ============================================
|
|
-- POPULATE INITIAL DATA
|
|
-- ============================================
|
|
|
|
-- Insert Categories
|
|
INSERT INTO category (name, slug, description) VALUES
|
|
('Shoes', 'shoes', 'Footwear for all occasions'),
|
|
('Shirts', 'shirts', 'T-shirts and casual tops'),
|
|
('Pants', 'pants', 'Jeans and trousers'),
|
|
('Hats', 'hats', 'Caps and beanies'),
|
|
('Accessories', 'accessories', 'Bags, belts, and more');
|
|
|
|
-- Insert Sample Products (Shoes - Featured)
|
|
INSERT INTO product (name, description, price, discount_price, category_id, gender, brand, sizes, colors, stock, images, is_featured, is_on_sale) VALUES
|
|
(
|
|
'Premium Running Shoes',
|
|
'High-performance running shoes with advanced cushioning technology',
|
|
129.99,
|
|
99.99,
|
|
1,
|
|
'men',
|
|
'Nike',
|
|
'["7", "8", "9", "10", "11", "12", "13"]',
|
|
'["Black", "White", "Blue"]',
|
|
50,
|
|
'["https://via.placeholder.com/300x300?text=Nike+Running"]',
|
|
TRUE,
|
|
TRUE
|
|
),
|
|
(
|
|
'Women Athletic Sneakers',
|
|
'Comfortable athletic sneakers for everyday wear',
|
|
99.99,
|
|
NULL,
|
|
1,
|
|
'women',
|
|
'Adidas',
|
|
'["5", "6", "7", "8", "9", "10"]',
|
|
'["Pink", "White", "Purple"]',
|
|
45,
|
|
'["https://via.placeholder.com/300x300?text=Adidas+Sneakers"]',
|
|
TRUE,
|
|
FALSE
|
|
),
|
|
(
|
|
'Basketball High Tops',
|
|
'Professional basketball shoes with ankle support',
|
|
149.99,
|
|
NULL,
|
|
1,
|
|
'men',
|
|
'Jordan',
|
|
'["8", "9", "10", "11", "12", "13"]',
|
|
'["Red", "Black", "Gold"]',
|
|
30,
|
|
'["https://via.placeholder.com/300x300?text=Jordan+High"]',
|
|
TRUE,
|
|
FALSE
|
|
),
|
|
(
|
|
'Casual Leather Loafers',
|
|
'Classic leather loafers for formal occasions',
|
|
139.99,
|
|
109.99,
|
|
1,
|
|
'men',
|
|
'Cole Haan',
|
|
'["7", "8", "9", "10", "11", "12"]',
|
|
'["Brown", "Black"]',
|
|
25,
|
|
'["https://via.placeholder.com/300x300?text=Cole+Haan+Loafers"]',
|
|
TRUE,
|
|
TRUE
|
|
),
|
|
(
|
|
'Hiking Boot Pro',
|
|
'Durable hiking boots with waterproof technology',
|
|
179.99,
|
|
149.99,
|
|
1,
|
|
'men',
|
|
'Salomon',
|
|
'["8", "9", "10", "11", "12", "13"]',
|
|
'["Brown", "Gray", "Black"]',
|
|
35,
|
|
'["https://via.placeholder.com/300x300?text=Salomon+Hiking"]',
|
|
TRUE,
|
|
TRUE
|
|
);
|
|
|
|
-- Insert Sample Products (Clothing)
|
|
INSERT INTO product (name, description, price, discount_price, category_id, gender, brand, sizes, colors, stock, images, is_featured, is_on_sale) VALUES
|
|
(
|
|
'Classic Cotton T-Shirt',
|
|
'Comfortable everyday cotton t-shirt',
|
|
29.99,
|
|
NULL,
|
|
2,
|
|
'men',
|
|
'Gap',
|
|
'["S", "M", "L", "XL", "XXL"]',
|
|
'["Red", "Blue", "White", "Black"]',
|
|
100,
|
|
'["https://via.placeholder.com/300x300?text=Cotton+Tee"]',
|
|
FALSE,
|
|
FALSE
|
|
),
|
|
(
|
|
'Silk Blouse',
|
|
'Elegant silk blouse for professional settings',
|
|
89.99,
|
|
69.99,
|
|
2,
|
|
'women',
|
|
'Hugo Boss',
|
|
'["XS", "S", "M", "L", "XL"]',
|
|
'["White", "Black", "Navy"]',
|
|
40,
|
|
'["https://via.placeholder.com/300x300?text=Silk+Blouse"]',
|
|
FALSE,
|
|
TRUE
|
|
),
|
|
(
|
|
'Slim Fit Jeans',
|
|
'Modern slim fit jeans with stretch fabric',
|
|
79.99,
|
|
59.99,
|
|
3,
|
|
'men',
|
|
'Levi''s',
|
|
'["28", "30", "32", "34", "36", "38"]',
|
|
'["Dark Blue", "Light Blue", "Black"]',
|
|
60,
|
|
'["https://via.placeholder.com/300x300?text=Slim+Jeans"]',
|
|
FALSE,
|
|
TRUE
|
|
),
|
|
(
|
|
'Yoga Leggings',
|
|
'High-waist yoga leggings with moisture-wicking',
|
|
89.99,
|
|
NULL,
|
|
3,
|
|
'women',
|
|
'Lululemon',
|
|
'["XS", "S", "M", "L", "XL"]',
|
|
'["Black", "Navy", "Purple", "Gray"]',
|
|
55,
|
|
'["https://via.placeholder.com/300x300?text=Yoga+Leggings"]',
|
|
TRUE,
|
|
FALSE
|
|
);
|
|
|
|
-- Insert Sample Users (for testing)
|
|
INSERT INTO "user" (email, hashed_password, full_name, phone, address, city, postal_code, country, is_active) VALUES
|
|
(
|
|
'user@example.com',
|
|
'$2b$12$KIXxPfROLqWHYIgC5FCOO.7yqKU8RvOmOhP7kJZnYLh6pJn2FSfKy', -- password: password123
|
|
'John Doe',
|
|
'1234567890',
|
|
'123 Main Street',
|
|
'New York',
|
|
'10001',
|
|
'USA',
|
|
TRUE
|
|
),
|
|
(
|
|
'jane@example.com',
|
|
'$2b$12$KIXxPfROLqWHYIgC5FCOO.7yqKU8RvOmOhP7kJZnYLh6pJn2FSfKy', -- password: password123
|
|
'Jane Smith',
|
|
'9876543210',
|
|
'456 Oak Avenue',
|
|
'Los Angeles',
|
|
'90001',
|
|
'USA',
|
|
TRUE
|
|
);
|
|
|
|
-- Create carts for users
|
|
INSERT INTO cart (user_id) VALUES
|
|
(1),
|
|
(2);
|
|
|
|
-- ============================================
|
|
-- SET PERMISSIONS
|
|
-- ============================================
|
|
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO ecommerce_user;
|
|
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO ecommerce_user;
|
|
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO ecommerce_user;
|
|
|
|
-- ============================================
|
|
-- COMPLETE
|
|
-- ============================================
|
|
-- Schema created successfully!
|
|
--
|
|
-- User: ecommerce_user
|
|
-- Database: ecommerce_db
|
|
-- Host: localhost:5432
|
|
--
|
|
-- Tables created: 9
|
|
-- - category, user, product, cart, cart_item
|
|
-- - order, order_item, user_wishlist, contact_message
|
|
--
|
|
-- Demo accounts:
|
|
-- - user@example.com / password123
|
|
-- - jane@example.com / password123
|
|
--
|
|
-- Sample data: 5 categories, 9 products, 2 users
|
|
--
|
|
-- Next steps:
|
|
-- 1. Update backend/.env with your credentials
|
|
-- 2. Run: uvicorn app.main:app --reload --port 8000
|