-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_setup.sql
More file actions
250 lines (230 loc) · 5.58 KB
/
database_setup.sql
File metadata and controls
250 lines (230 loc) · 5.58 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
-- =====================================================
-- E-Commerce API Database Setup Script
-- =====================================================
-- This script creates the database and all required tables
-- Run this before starting the application
-- =====================================================
-- Create database if it doesn't exist
CREATE
DATABASE IF NOT EXISTS ecommerce_db
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
-- Use the database
USE
ecommerce_db;
-- =====================================================
-- Drop existing tables (for clean setup)
-- =====================================================
-- Uncomment these lines if you want to reset the database
-- DROP TABLE IF EXISTS products;
-- DROP TABLE IF EXISTS users;
-- =====================================================
-- Create Users Table
-- =====================================================
CREATE TABLE IF NOT EXISTS users
(
id
INT
AUTO_INCREMENT
PRIMARY
KEY,
name
VARCHAR
(
150
) NOT NULL,
email VARCHAR
(
255
) NOT NULL UNIQUE,
password VARCHAR
(
255
) NOT NULL,
role ENUM
(
'buyer',
'seller',
'admin'
) NOT NULL DEFAULT 'buyer',
is_active TINYINT
(
1
) NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-- Indexes for performance
INDEX idx_email
(
email
),
INDEX idx_role
(
role
),
INDEX idx_is_active
(
is_active
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_unicode_ci;
-- =====================================================
-- Create Products Table
-- =====================================================
CREATE TABLE IF NOT EXISTS products
(
id
INT
AUTO_INCREMENT
PRIMARY
KEY,
name
VARCHAR
(
150
) NOT NULL,
description TEXT,
price DECIMAL
(
10,
2
) NOT NULL,
stock INT NOT NULL DEFAULT 0,
seller_id INT NOT NULL,
is_active TINYINT
(
1
) NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-- Foreign key constraint
CONSTRAINT fk_seller
FOREIGN KEY
(
seller_id
)
REFERENCES users
(
id
)
ON DELETE CASCADE,
-- Indexes for performance
INDEX idx_name
(
name
),
INDEX idx_seller_id
(
seller_id
),
INDEX idx_is_active
(
is_active
),
INDEX idx_price
(
price
),
INDEX idx_created_at
(
created_at
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE =utf8mb4_unicode_ci;
-- =====================================================
-- Insert Sample Data (Optional)
-- =====================================================
-- Uncomment the following section to insert sample users and products
-- Sample Users (passwords are hashed with PBKDF2-SHA256)
-- Password for all: Admin1234, Seller1234, Buyer1234
/*
INSERT INTO users (name, email, password, role) VALUES
(
'Admin User',
'admin@test.com',
'$pbkdf2-sha256$29000$1trb.39PaS2ltBbCeO8dIw$xQZvJhQJ5nE8KmGFJQFJ9xLQJ5nE8KmGFJQFJ9xLQ',
'admin'
),
(
'Seller User',
'seller@test.com',
'$pbkdf2-sha256$29000$2tr7.59PaS3ltBbCeO9dIw$yRZwJhRJ5nE9KmGFJRFJ9xLRJ5nE9KmGFJRFJ9xLR',
'seller'
),
(
'Buyer User',
'buyer@test.com',
'$pbkdf2-sha256$29000$3ts8.69PaS4ltBbCeP0dIw$zSZxJhSJ5nF0KmGFJSFJ9xLSJ5nF0KmGFJSFJ9xLS',
'buyer'
);
-- Sample Products
INSERT INTO products (name, description, price, stock, seller_id) VALUES
(
'Laptop',
'High-performance gaming laptop with RTX 4060',
1299.99,
10,
2
),
(
'Smartphone',
'Latest flagship smartphone with 5G connectivity',
899.99,
25,
2
),
(
'Wireless Headphones',
'Premium noise-cancelling wireless headphones',
299.99,
50,
2
),
(
'Mechanical Keyboard',
'RGB mechanical keyboard with Cherry MX switches',
149.99,
30,
2
),
(
'Gaming Mouse',
'Professional gaming mouse with 16000 DPI',
79.99,
40,
2
);
*/
-- =====================================================
-- Verification Queries
-- =====================================================
-- Uncomment to verify the setup
-- Check users table structure
-- DESCRIBE users;
-- Check products table structure
-- DESCRIBE products;
-- Count records
-- SELECT COUNT(*) as total_users FROM users;
-- SELECT COUNT(*) as total_products FROM products;
-- View all users
-- SELECT id, name, email, role, is_active, created_at FROM users;
-- View all products
-- SELECT id, name, price, stock, seller_id, is_active FROM products;
-- =====================================================
-- Database Information
-- =====================================================
SELECT 'Database setup completed successfully!' as status,
DATABASE() as current_database,
VERSION() as mysql_version,
NOW() as setup_time;
-- Show tables
SHOW
TABLES;
-- =====================================================
-- Notes:
-- =====================================================
-- 1. The application (SQLAlchemy) will also create tables automatically
-- 2. Sample data is commented out - uncomment if needed
-- 3. Password hashes in sample data are examples only
-- 4. Always use strong passwords in production
-- 5. Foreign key constraints ensure data integrity
-- 6. Indexes are created for commonly queried fields
-- =====================================================