-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sql
More file actions
263 lines (195 loc) · 6.37 KB
/
setup.sql
File metadata and controls
263 lines (195 loc) · 6.37 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
-- DROP TABLE commands:
DROP TABLE IF EXISTS reviews;
DROP TABLE IF EXISTS purchases;
DROP TABLE IF EXISTS employee_log;
DROP TABLE IF EXISTS requests;
DROP TABLE IF EXISTS lego_parts;
DROP TABLE IF EXISTS lego_sets;
DROP TABLE IF EXISTS themes;
DROP TABLE IF EXISTS categories;
DROP TABLE IF EXISTS product_inventory;
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS discounts;
DROP TABLE IF EXISTS employees;
-- CREATE TABLE commands:
-- Themes for the lego sets.
CREATE TABLE themes (
-- Unique ID for each theme.
theme_id INT PRIMARY KEY,
-- Name of the theme.
theme_name VARCHAR(70) NOT NULL,
-- Larger theme (can be NULL).
parent_id INT
);
-- Categories for the lego parts.
CREATE TABLE categories (
-- Unique ID for each category.
category_id INT PRIMARY KEY,
-- Name of the category.
category_name VARCHAR(100) NOT NULL
);
CREATE TABLE product_inventory (
-- Unique ID for each product.
product_id INT PRIMARY KEY,
-- Price for each product.
product_price NUMERIC(6, 2) NOT NULL,
-- Name of each product.
product_name VARCHAR(255) NOT NULL,
-- Quantity of the product inventory.
quantity INT NOT NULL
);
-- All lego set information.
CREATE TABLE lego_sets (
-- Unique ID for each product.
product_id INT PRIMARY KEY,
-- Number of parts in the set.
num_parts INT NOT NULL,
-- Estimated time, in minutes,
-- to complete the set.
time_to_complete INT NOT NULL,
-- Year the set was released.
year_released YEAR NOT NULL,
-- Theme of the set.
theme_id INT,
-- Specialization of product.
-- No cascade on UPDATE because product IDs don't change often.
FOREIGN KEY (product_id) REFERENCES product_inventory(product_id)
ON DELETE CASCADE,
FOREIGN KEY (theme_id) REFERENCES themes(theme_id)
ON DELETE CASCADE
);
-- All lego parts information.
CREATE TABLE lego_parts (
-- Unique ID for each product.
product_id INT PRIMARY KEY,
-- Category of the part.
category_id INT,
-- Specialization of product.
FOREIGN KEY (product_id) REFERENCES product_inventory(product_id)
ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES categories(category_id)
ON DELETE CASCADE
);
-- Discounts based on membership level.
CREATE TABLE discounts (
-- Type of member.
-- VIP : 'V', Regular: 'R'
member_type CHAR(1) PRIMARY KEY,
-- Discount amount.
-- 10 represents 10% etc.
discount_amount INT NOT NULL,
CHECK(member_type IN ('V', 'R')),
CHECK(discount_amount >=0 AND discount_amount <= 100)
);
-- Registered customers.
CREATE TABLE customers (
-- Unique username for each customer.
customer_username VARCHAR(50) PRIMARY KEY,
-- Customer first and last name.
customer_name VARCHAR(100) NOT NULL,
-- Customer email.
customer_email VARCHAR(50) NOT NULL,
-- Type of member.
-- VIP : 'V', Regular: 'R'
member_type CHAR(1),
FOREIGN KEY (member_type) REFERENCES discounts(member_type)
ON UPDATE CASCADE
ON DELETE CASCADE
);
-- Purchases made by customers.
-- Each purchase is only ONE item.
CREATE TABLE purchases (
-- Unique ID for each purchase.
purchase_id SERIAL PRIMARY KEY,
-- Product that was purchased.
product_id INT,
-- Customer that made the purchase.
customer_username VARCHAR(50),
-- Purchased item total.
-- Because customers often get discounts,
-- this keeps track of historical purchase totals in case
-- discount amounts change.
purchase_item_total NUMERIC(6, 2) NOT NULL,
-- Time of purchase.
purchase_time TIMESTAMP NOT NULL,
FOREIGN KEY (product_id) REFERENCES product_inventory(product_id)
ON DELETE CASCADE,
FOREIGN KEY (customer_username) REFERENCES customers(customer_username)
ON DELETE CASCADE
);
-- Reviews made for purchases.
-- Weak entity.
CREATE TABLE reviews (
-- Review is uniquely identified by a purchase ID.
-- Chose not to merge the two tables.
-- Not using SERIAL again because we're not auto-incrementing this.
purchase_id BIGINT UNSIGNED PRIMARY KEY,
-- Customer that made the review.
customer_username VARCHAR(50),
-- Time of review.
review_time TIMESTAMP NOT NULL,
-- Rating given on a scale from 1-5.
rating INT NOT NULL,
-- Short review box.
-- Can be NULL.
review VARCHAR(500),
-- Review is only for a given purchase.
FOREIGN KEY (purchase_id) REFERENCES purchases(purchase_id)
ON DELETE CASCADE,
-- Included so that we can retrieve customer name.
FOREIGN KEY (customer_username) REFERENCES customers(customer_username)
ON UPDATE CASCADE
ON DELETE CASCADE,
-- Restrict the range of ratings to 1-5.
CHECK (rating <= 5 AND rating >= 1)
);
-- Request made for ONE product.
-- Possible idea: Check that each product requested has 0 inventory.
CREATE TABLE requests (
-- Unique ID for each request.
request_id SERIAL PRIMARY KEY,
-- Product that request was made for.
product_id INT,
-- Customer that made the purchase.
customer_username VARCHAR(50),
-- Status of the request.
-- Fulfilled: 'F', In Progress: 'P', Unfulfilled: 'U'
request_status CHAR(1) NOT NULL,
FOREIGN KEY (product_id) REFERENCES product_inventory(product_id)
ON DELETE CASCADE,
FOREIGN KEY (customer_username) REFERENCES customers(customer_username)
ON UPDATE CASCADE
ON DELETE CASCADE,
-- Check that the status is valid.
CHECK(request_status IN ('F', 'P', 'U'))
);
-- Employees that can manage the database and fulfill requests.
CREATE TABLE employees (
-- Unique username for each employee.
employee_username VARCHAR(50) PRIMARY KEY,
-- Employee Name.
employee_name VARCHAR(100) NOT NULL,
-- Permissions.
-- Requests read: 'R', write: 'W', readwrite: 'RW'
employee_permissions VARCHAR(2) NOT NULL,
CHECK ((employee_permissions) IN ('R', 'W', 'RW'))
);
-- Employee log of requests that are fulfilled.
CREATE TABLE employee_log (
-- Request that was fulfilled.
request_id BIGINT UNSIGNED,
-- Employee that made the change.
employee_username VARCHAR(50),
-- Time of change.
log_time TIMESTAMP NOT NULL,
-- Brief description of change that was made.
change_made VARCHAR(255) NOT NULL,
PRIMARY KEY (request_id, employee_username),
FOREIGN KEY (request_id) REFERENCES requests(request_id)
ON DELETE CASCADE,
FOREIGN KEY (employee_username) REFERENCES employees(employee_username)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE INDEX idx_theme_name ON themes(theme_name);
CREATE INDEX idx_prod_price ON product_inventory(product_price);