-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecommerce_schema.sql
More file actions
119 lines (112 loc) · 4.52 KB
/
ecommerce_schema.sql
File metadata and controls
119 lines (112 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
-- Drop existing tables if they exist (in reverse order of dependencies)
DROP TABLE IF EXISTS order_items CASCADE;
DROP TABLE IF EXISTS reviews CASCADE;
DROP TABLE IF EXISTS orders CASCADE;
DROP TABLE IF EXISTS products CASCADE;
DROP TABLE IF EXISTS categories CASCADE;
DROP TABLE IF EXISTS users CASCADE;
-- 1. Users Table
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
username VARCHAR(100) UNIQUE NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
phone_number VARCHAR(20),
date_of_birth DATE,
gender VARCHAR(10),
city VARCHAR(100),
state_province VARCHAR(100),
country_code VARCHAR(2) DEFAULT 'US',
account_status VARCHAR(20) DEFAULT 'active',
total_orders INTEGER DEFAULT 0,
total_spent DECIMAL(10,2) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 2. Categories Table (Hierarchical structure for complex queries)
CREATE TABLE categories (
category_id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
slug VARCHAR(100) UNIQUE NOT NULL,
description TEXT,
parent_category_id INTEGER REFERENCES categories(category_id),
is_active BOOLEAN DEFAULT TRUE,
product_count INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 3. Products Table (Complex attributes for aggregations and analysis)
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
sku VARCHAR(100) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL,
slug VARCHAR(255) UNIQUE NOT NULL,
description TEXT,
short_description VARCHAR(500),
category_id INTEGER NOT NULL REFERENCES categories(category_id),
brand VARCHAR(100),
price DECIMAL(10,2) NOT NULL,
cost DECIMAL(10,2),
weight_kg DECIMAL(10,3),
stock_quantity INTEGER NOT NULL DEFAULT 0,
is_active BOOLEAN DEFAULT TRUE,
is_featured BOOLEAN DEFAULT FALSE,
warranty_months INTEGER DEFAULT 12,
rating_average DECIMAL(3,2) DEFAULT 0,
rating_count INTEGER DEFAULT 0,
total_sales INTEGER DEFAULT 0,
revenue_generated DECIMAL(12,2) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 4. Orders Table (Complex order management with multiple statuses and relationships)
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
order_number VARCHAR(50) UNIQUE NOT NULL,
user_id INTEGER NOT NULL REFERENCES users(user_id),
order_status VARCHAR(20) NOT NULL DEFAULT 'pending', -- pending, processing, shipped, delivered, cancelled, refunded
payment_status VARCHAR(20) NOT NULL DEFAULT 'pending', -- pending, paid, failed, refunded
shipping_address TEXT,
subtotal DECIMAL(10,2) NOT NULL,
tax_amount DECIMAL(10,2) NOT NULL DEFAULT 0,
shipping_cost DECIMAL(10,2) NOT NULL DEFAULT 0,
discount_amount DECIMAL(10,2) NOT NULL DEFAULT 0,
total_amount DECIMAL(10,2) NOT NULL,
payment_method VARCHAR(50), -- credit_card, paypal, bank_transfer
shipping_method VARCHAR(50), -- standard, express, overnight
customer_notes TEXT,
tracking_number VARCHAR(100),
shipped_at TIMESTAMP,
delivered_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 5. Order Items Table (Junction table for order-product relationships)
CREATE TABLE order_items (
order_item_id SERIAL PRIMARY KEY,
order_id INTEGER NOT NULL REFERENCES orders(order_id) ON DELETE CASCADE,
product_id INTEGER NOT NULL REFERENCES products(product_id),
quantity INTEGER NOT NULL,
unit_price DECIMAL(10,2) NOT NULL,
discount_amount DECIMAL(10,2) DEFAULT 0,
tax_amount DECIMAL(10,2) DEFAULT 0,
total_price DECIMAL(10,2) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 6. Reviews Table (Complex review system with embeddings for semantic search)
CREATE TABLE reviews (
review_id SERIAL PRIMARY KEY,
product_id INTEGER NOT NULL REFERENCES products(product_id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(user_id),
order_id INTEGER REFERENCES orders(order_id),
rating INTEGER NOT NULL CHECK (rating >= 1 AND rating <= 5),
title VARCHAR(200),
comment TEXT,
comment_embedding VECTOR(1024), -- Vector embedding for semantic search
pros TEXT,
cons TEXT,
is_verified_purchase BOOLEAN DEFAULT FALSE,
helpful_count INTEGER DEFAULT 0,
status VARCHAR(20) DEFAULT 'approved', -- pending, approved, rejected
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);