- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB-task_script.sql
More file actions
Latest commit
258 lines (207 loc) · 6.57 KB
/
Copy pathDB-task_script.sql
File metadata and controls
258 lines (207 loc) · 6.57 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
CREATEDATABASEfactory;
USE factory;
-- CREATING TABLES
CREATETABLEproducts(
id intNOT NULL AUTO_INCREMENT UNIQUE,
name varchar(255) NOT NULL,
shortDescription text DEFAULT 'none',
PRIMARY KEY (id)
);
CREATETABLEproducts_details(
id intNOT NULL UNIQUE,
weight float DEFAULT 0.00,
barcodeNumber varchar(20),
price decimal(5, 2),
PRIMARY KEY (id),
FOREIGN KEY (id) REFERENCES products(id)
);
CREATETABLEcustomers (
id intNOT NULL AUTO_INCREMENT UNIQUE,
financialId varchar(255),
vatNumber varchar(255),
PRIMARY KEY (id)
);
CREATETABLEcustomers_details (
id intNOT NULL UNIQUE,
name varchar(255) NOT NULL,
address textNOT NULL,
contactPerson varchar(255),
PRIMARY KEY (id),
FOREIGN KEY (id) REFERENCES customers(id)
);
CREATETABLEorders (
id intNOT NULL AUTO_INCREMENT UNIQUE,
orderDate dateNOT NULL,
totalPrice decimal(5, 2) NOT NULL,
customerId intNOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (customerId) REFERENCES customers(id)
);
CREATETABLEproducts_orders (
productId intNOT NULL,
orderId intNOT NULL,
productQuantity intNOT NULL DEFAULT 0,
PRIMARY KEY (productId, orderId),
FOREIGN KEY (productId) REFERENCES products(id),
FOREIGN KEY (orderId) REFERENCES orders(id)
);
CREATETABLEactivity_log (
id intNOT NULL AUTO_INCREMENT UNIQUE,
username varchar(255) DEFAULT 'Anonymous',
description text,
editedCustomerId int DEFAULT NULL,
editedProductId int DEFAULT NULL,
editedOrderId int DEFAULT NULL,
date_time Datetime DEFAULT CURRENT_TIMESTAMP(),
PRIMARY KEY (id),
FOREIGN KEY (editedCustomerId) REFERENCES customers(id),
FOREIGN KEY (editedProductId) REFERENCES products(id),
FOREIGN KEY (editedOrderId) REFERENCES orders(id)
);
-- populate data in Tables
INSERT INTO products (id, name, shortDescription)
VALUES
(1, 'chair', 'one very nice chair'),
(2, 'table', 'very nice kitchen table'),
(3, 'wardrobe', 'really large wardrobe'),
(4, 'sofa', 'a comforTABLE sofa'),
(5, 'bed', 'one premium size bed');
INSERT INTO products_details (id, weight, barcodeNumber, price)
VALUES
(1, 4.850, '012356745650', 25.50),
(2, 10.360, '012345675650', 225.50),
(3, 32.100, '012345674650', 485.50),
(4, 15.685, '012346745650', 350.99),
(5, 50.950, '012345674565', 860.99);
INSERT INTO customers (id, financialId, vatNumber)
VALUES
(11, '', NULL),
(12, '000459797', NULL),
(13, NULL, '584635416');
INSERT INTO customers_details (id, name, address, contactPerson)
VALUES
(11, 'Flowers LLC', 'Pleven, ul. Skopie 56', 'Jane Doe'),
(12, 'IBM LTD', 'Sofia, ul. Tsarigradsko shose 256', 'Anna Georgieva'),
(13, 'Evil Corp.', 'New York, bul. Bulgaria 001', NULL);
INSERT INTO orders (id, orderDate, totalPrice, customerId)
VALUES
(101, '2020-03-02', 0.2, 11),
(102, '2020-03-20', 2500, 12);
INSERT INTO products_orders (productId, orderId, productQuantity)
VALUES
(1, 101, 36),
(2, 101, 6),
(5, 101, 1),
(1, 102, 36),
(2, 102, 6),
(4, 102, 1);
INSERT INTO activity_log (description, editedCustomerId, date_time)
VALUES
('nothing happened', 12, CURRENT_TIMESTAMP());
-- CREATE some Views
CREATEVIEWget_products_dataAS
SELECTp.id, p.name, p.shortDescription, pd.weight, pd.price, pd.barcodeNumberFROM products AS p
JOIN products_details AS pd
ONp.id=pd.id;
SELECT*FROM get_products_data;
CREATEVIEWget_customers_dataAS
SELECTc.id, cd.name, c.financialId, c.vatNumber, cd.address, cd.contactPersonFROM customers c
JOIN customers_details cd
ONc.id=cd.id;
SELECT*FROM get_customers_data;
-- CREATE search queries
USE factory;
DROP PROCEDURE IF EXISTS get_product_info_by_part_product_name;
DELIMITER $$
CREATE PROCEDURE get_product_info_by_part_product_name
(
product_name varchar(255)
)
BEGIN
SELECT*FROM get_products_data
WHERE name LIKE concat('%', product_name, '%');
end$$
DELIMITER ;
CALL get_product_info_by_part_product_name('hai');
-- DROP PROCEDURE get_product_info_by_part_product_name;
CREATE PROCEDURE get_order_info_by_order_number
(
order_number int
)
BEGIN
SELECTpo.orderId, c.vatNumber, cd.name, p.name, po.productQuantity, pd.priceAS'price per unit', pd.price*po.productQuantityAS'total price'FROM products_orders po
JOIN products p
ONp.id=po.productId
JOIN products_details pd
ONp.id=pd.id
JOIN orders o
ONo.id=po.orderId
JOIN customers c
ONc.id=o.customerId
JOIN customers_details cd
ONcd.id=c.id
WHEREpo.orderId= order_number;
END;
CALL get_order_info_by_order_number(101);
-- DROP PROCEDURE get_order_info_by_order_number;
CREATE PROCEDURE get_orders_for_last_month_by_customer_name
(
customer_name varchar(255)
)
BEGIN
SELECTo.idAS'ORDER id', o.totalPrice, o.orderDate, cd.nameAS'customer name'FROM orders o
JOIN customers_details cd
ONcd.id=o.customerId
WHEREcd.nameLIKE concat('%', customer_name, '%')
AND DATE_ADD(o.orderDate, INTERVAL 1 Month) >current_date();
END;
CALL get_orders_for_last_month_by_customer_name('flower');
-- DROP PROCEDURE get_orders_for_last_month_by_customer_name;
CREATEVIEWget_products_sold_last_monthAS
SELECTp.nameAS'product.name', sum(po.productQuantity) AS'total quantity sold last month'FROM products_orders po
JOIN products_details pd
ONpd.id=po.productId
JOIN products p
ONp.id=pd.id
JOIN orders o
ONo.id=po.orderId
WHERE DATE_ADD(o.orderDate, INTERVAL 1 Month) >current_date()
GROUP BYpd.id
ORDER BYp.name;
-- DROP view get_products_sold_last_month;
SELECT*FROM get_products_sold_last_month;
-- creade indices
CREATEINDEXindex_products
ON products_details (id);
CREATEINDEXindex_orders
ON products_orders (orderId);
-- log info in logs table
CREATE PROCEDURE insert_in_log_table(msg text, customerId int, productId int, orderId int)
BEGIN
IF customerId IS NOT NULL THEN
IF (SELECTcount(*) FROM customers
WHEREcustomers.id= customerId) >0 THEN
INSERT INTO activity_log (description, editedCustomerId) VALUES (msg, customerId);
END IF;
END IF;
IF productId IS NOT NULL THEN
IF (SELECTcount(*) FROM products
WHEREproducts.id= productId) >0 THEN
INSERT INTO activity_log (description, editedProductId) VALUES (msg, productId);
END IF;
END IF;
IF orderId IS NOT NULL THEN
IF (SELECTcount(*) FROM orders
WHEREorders.id= orderId) >0 THEN
INSERT INTO activity_log (description, editedOrderId) VALUES (msg, orderId);
END IF;
END IF;
END;
-- DROP PROCEDURE insert_in_log_table;
CALL insert_in_log_table('log edited', 00000, 1, 00000);
-- DROP DATABASE factory;
-- DROP TABLE products;
-- DROP TABLE customers;
-- DROP TABLE orders;
-- DROP TABLE products_orders;
-- DROP TABLE activity_log;