-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
366 lines (336 loc) · 13.2 KB
/
Copy pathschema.sql
File metadata and controls
366 lines (336 loc) · 13.2 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
-- Database Schema with 30 Tables
-- Some tables have relations with 5 other tables
-- Core Reference Tables
CREATE TABLE IF NOT EXISTS `countries` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`code` VARCHAR(2) NOT NULL UNIQUE,
`name` VARCHAR(100) NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS `categories` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(100) NOT NULL,
`description` TEXT,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS `payment_methods` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(50) NOT NULL,
`is_active` BOOLEAN DEFAULT TRUE,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS `order_statuses` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(50) NOT NULL,
`description` TEXT,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS `shipping_statuses` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(50) NOT NULL,
`description` TEXT,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- User Related Tables
CREATE TABLE IF NOT EXISTS `users` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`first_name` VARCHAR(100) NOT NULL,
`last_name` VARCHAR(100) NOT NULL,
`email` VARCHAR(255) NOT NULL UNIQUE,
`phone` VARCHAR(20),
`country_id` INT,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`country_id`) REFERENCES `countries`(`id`) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS `user_addresses` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`street` VARCHAR(255) NOT NULL,
`city` VARCHAR(100) NOT NULL,
`state` VARCHAR(100),
`postal_code` VARCHAR(20),
`country_id` INT,
`is_default` BOOLEAN DEFAULT FALSE,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`country_id`) REFERENCES `countries`(`id`) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS `user_preferences` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL UNIQUE,
`language` VARCHAR(10) DEFAULT 'en',
`currency` VARCHAR(3) DEFAULT 'USD',
`timezone` VARCHAR(50),
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
);
-- Product Related Tables
CREATE TABLE IF NOT EXISTS `products` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(255) NOT NULL,
`description` TEXT,
`category_id` INT,
`price_cents` INT NOT NULL,
`stock_quantity` INT DEFAULT 0,
`is_active` BOOLEAN DEFAULT TRUE,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`category_id`) REFERENCES `categories`(`id`) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS `product_images` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`product_id` INT NOT NULL,
`image_url` VARCHAR(500) NOT NULL,
`is_primary` BOOLEAN DEFAULT FALSE,
`display_order` INT DEFAULT 0,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `product_attributes` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`product_id` INT NOT NULL,
`attribute_name` VARCHAR(100) NOT NULL,
`attribute_value` VARCHAR(255) NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE
);
-- Order Related Tables
CREATE TABLE IF NOT EXISTS `orders` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`status_id` INT NOT NULL,
`total_cents` INT NOT NULL,
`shipping_address_id` INT,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`status_id`) REFERENCES `order_statuses`(`id`),
FOREIGN KEY (`shipping_address_id`) REFERENCES `user_addresses`(`id`) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS `order_items` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`order_id` INT NOT NULL,
`product_id` INT NOT NULL,
`quantity` INT NOT NULL,
`price_cents` INT NOT NULL,
`subtotal_cents` INT NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `payments` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`order_id` INT NOT NULL,
`payment_method_id` INT NOT NULL,
`amount_cents` INT NOT NULL,
`transaction_id` VARCHAR(255),
`status` VARCHAR(50) DEFAULT 'pending',
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`payment_method_id`) REFERENCES `payment_methods`(`id`)
);
CREATE TABLE IF NOT EXISTS `shipments` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`order_id` INT NOT NULL,
`shipping_status_id` INT NOT NULL,
`tracking_number` VARCHAR(100),
`carrier` VARCHAR(100),
`shipped_at` TIMESTAMP NULL,
`delivered_at` TIMESTAMP NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`shipping_status_id`) REFERENCES `shipping_statuses`(`id`)
);
-- Review and Rating Tables
CREATE TABLE IF NOT EXISTS `reviews` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`product_id` INT NOT NULL,
`order_id` INT,
`rating` INT NOT NULL CHECK (`rating` BETWEEN 1 AND 5),
`title` VARCHAR(255),
`comment` TEXT,
`is_verified_purchase` BOOLEAN DEFAULT FALSE,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS `review_likes` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`review_id` INT NOT NULL,
`user_id` INT NOT NULL,
`is_like` BOOLEAN DEFAULT TRUE,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`review_id`) REFERENCES `reviews`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
UNIQUE KEY `unique_review_user` (`review_id`, `user_id`)
);
-- Cart and Wishlist Tables
CREATE TABLE IF NOT EXISTS `carts` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL UNIQUE,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `cart_items` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`cart_id` INT NOT NULL,
`product_id` INT NOT NULL,
`quantity` INT NOT NULL DEFAULT 1,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`cart_id`) REFERENCES `carts`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE,
UNIQUE KEY `unique_cart_product` (`cart_id`, `product_id`)
);
CREATE TABLE IF NOT EXISTS `wishlists` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`name` VARCHAR(100) DEFAULT 'My Wishlist',
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `wishlist_items` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`wishlist_id` INT NOT NULL,
`product_id` INT NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`wishlist_id`) REFERENCES `wishlists`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE,
UNIQUE KEY `unique_wishlist_product` (`wishlist_id`, `product_id`)
);
-- Coupon and Discount Tables
CREATE TABLE IF NOT EXISTS `coupons` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`code` VARCHAR(50) NOT NULL UNIQUE,
`discount_type` VARCHAR(20) NOT NULL,
`discount_value` INT NOT NULL,
`min_purchase_cents` INT DEFAULT 0,
`max_discount_cents` INT,
`valid_from` TIMESTAMP NOT NULL,
`valid_until` TIMESTAMP NOT NULL,
`usage_limit` INT,
`used_count` INT DEFAULT 0,
`is_active` BOOLEAN DEFAULT TRUE,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS `order_coupons` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`order_id` INT NOT NULL,
`coupon_id` INT NOT NULL,
`discount_amount_cents` INT NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`coupon_id`) REFERENCES `coupons`(`id`) ON DELETE CASCADE
);
-- Notification Tables
CREATE TABLE IF NOT EXISTS `notifications` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`type` VARCHAR(50) NOT NULL,
`title` VARCHAR(255) NOT NULL,
`message` TEXT,
`is_read` BOOLEAN DEFAULT FALSE,
`related_order_id` INT,
`related_product_id` INT,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`related_order_id`) REFERENCES `orders`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`related_product_id`) REFERENCES `products`(`id`) ON DELETE SET NULL
);
-- Analytics and Tracking Tables
CREATE TABLE IF NOT EXISTS `product_views` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`product_id` INT NOT NULL,
`user_id` INT,
`ip_address` VARCHAR(45),
`user_agent` TEXT,
`viewed_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS `search_queries` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT,
`query` VARCHAR(255) NOT NULL,
`results_count` INT DEFAULT 0,
`searched_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL
);
-- Complex Junction Table with 5 Relations
-- This table relates orders, products, users, payments, and shipments
CREATE TABLE IF NOT EXISTS `order_fulfillment_details` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`order_id` INT NOT NULL,
`product_id` INT NOT NULL,
`user_id` INT NOT NULL,
`payment_id` INT,
`shipment_id` INT,
`quantity_fulfilled` INT NOT NULL DEFAULT 0,
`fulfillment_status` VARCHAR(50) DEFAULT 'pending',
`notes` TEXT,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`payment_id`) REFERENCES `payments`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`shipment_id`) REFERENCES `shipments`(`id`) ON DELETE SET NULL
);
-- Another Complex Table with 5 Relations
-- This table relates users, products, categories, orders, and reviews
CREATE TABLE IF NOT EXISTS `user_product_interactions` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`product_id` INT NOT NULL,
`category_id` INT,
`order_id` INT,
`review_id` INT,
`interaction_type` VARCHAR(50) NOT NULL,
`metadata` JSON,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`category_id`) REFERENCES `categories`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`review_id`) REFERENCES `reviews`(`id`) ON DELETE SET NULL
);
-- Another Complex Table with 5 Relations
-- This table relates orders, payments, shipments, coupons, and users
CREATE TABLE IF NOT EXISTS `order_analytics` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`order_id` INT NOT NULL,
`payment_id` INT,
`shipment_id` INT,
`coupon_id` INT,
`user_id` INT NOT NULL,
`revenue_cents` INT NOT NULL,
`discount_cents` INT DEFAULT 0,
`shipping_cents` INT DEFAULT 0,
`tax_cents` INT DEFAULT 0,
`profit_cents` INT,
`analytics_date` DATE NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`payment_id`) REFERENCES `payments`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`shipment_id`) REFERENCES `shipments`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`coupon_id`) REFERENCES `coupons`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
);
-- Another Complex Table with 5 Relations
-- This table relates products, categories, users, orders, and reviews for recommendations
CREATE TABLE IF NOT EXISTS `product_recommendations` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`product_id` INT NOT NULL,
`category_id` INT,
`user_id` INT,
`order_id` INT,
`review_id` INT,
`recommendation_score` DECIMAL(5,2) NOT NULL,
`recommendation_type` VARCHAR(50) NOT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`category_id`) REFERENCES `categories`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`review_id`) REFERENCES `reviews`(`id`) ON DELETE SET NULL
);