-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe-commerce.sql
More file actions
88 lines (70 loc) · 2.08 KB
/
e-commerce.sql
File metadata and controls
88 lines (70 loc) · 2.08 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
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
full_name VARCHAR(120) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
phone VARCHAR(20),
created_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE addresses (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT REFERENCES users(id),
address_line VARCHAR(255) NOT NULL,
city VARCHAR(100),
country VARCHAR(100),
postal_code VARCHAR(20),
is_default BOOLEAN DEFAULT FALSE
);
CREATE TABLE categories (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(120) NOT NULL,
parent_id BIGINT REFERENCES categories(id)
);
CREATE TABLE products (
id BIGSERIAL PRIMARY KEY,
category_id BIGINT REFERENCES categories(id),
name VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE product_variants (
id BIGSERIAL PRIMARY KEY,
product_id BIGINT REFERENCES products(id),
sku VARCHAR(60) UNIQUE NOT NULL,
price DECIMAL(10,2) NOT NULL,
stock INT DEFAULT 0,
color VARCHAR(60),
size VARCHAR(60)
);
CREATE TABLE orders (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT REFERENCES users(id),
status VARCHAR(50) DEFAULT 'pending',
total_amount DECIMAL(10,2) NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE order_items (
id BIGSERIAL PRIMARY KEY,
order_id BIGINT REFERENCES orders(id),
variant_id BIGINT REFERENCES product_variants(id),
quantity INT NOT NULL,
price DECIMAL(10,2) NOT NULL
);
CREATE TABLE payments (
id BIGSERIAL PRIMARY KEY,
order_id BIGINT REFERENCES orders(id),
amount DECIMAL(10,2) NOT NULL,
provider VARCHAR(50), -- e.g., PayPal, Stripe, Wing, ABA
status VARCHAR(50) DEFAULT 'pending',
paid_at TIMESTAMP
);
-- 🛒 E-Commerce Database Structure
-- Main Entities
-- users — customer accounts
-- products — items you sell
-- product_variants — sizes/colors/models
-- categories — product grouping
-- orders — customer purchases
-- order_items — items inside each order
-- payments — payment records
-- addresses — shipping/billing addresses