This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plcs.sql
431 lines (369 loc) · 12.8 KB
/
plcs.sql
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jun 25, 2017 at 11:23 AM
-- Server version: 10.1.22-MariaDB
-- PHP Version: 7.1.4
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `plcs`
--
-- --------------------------------------------------------
--
-- Table structure for table `customer`
--
CREATE TABLE `customer` (
`CustomerID` int(11) NOT NULL,
`Barcode` bigint(20) NOT NULL,
`FirstName` varchar(15) NOT NULL,
`LastName` varchar(15) NOT NULL,
`Email` varchar(30) NOT NULL,
`Password` varchar(255) NOT NULL,
`Points` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `customer`
--
INSERT INTO `customer` (`CustomerID`, `Barcode`, `FirstName`, `LastName`, `Email`, `Password`, `Points`) VALUES
(1, 100000, 'u1', 'user', '[email protected]', '24c9e15e52afc47c225b757e7bee1f9d', 22),
(2, 100001, 'u2', 'user', '[email protected]', '7e58d63b60197ceb55a1c487989a3720', 0),
(3, 100002, 'u3', 'user', '[email protected]', '92877af70a45fd6a2ed7fe81e1236b78', 0),
(4, 100003, 'Sorath', 'Asnani', '[email protected]', 'aad75c0f494544bb687a55a3ba4fa064', 0),
(5, 100004, 'Martino', 'Mensio', '[email protected]', 'a5c36f67faa5284c625a73b7ea473feb', 0),
(6, 100005, 'Giuseppe', 'Carella', '[email protected]', '3e6a90c7a131518bfe47b04d67c0b4a2', 0),
(7, 100006, 'MSG', 'Admin', '[email protected]', '6e2baaf3b97dbeef01c0043275f9a0e7', 0);
--
-- Triggers `customer`
--
DELIMITER $$
CREATE TRIGGER `before_customer_insert` BEFORE INSERT ON `customer` FOR EACH ROW BEGIN
DECLARE newBarcode bigint default 100000;
IF (SELECT count(*) FROM customer) = 0 THEN
SET NEW.Barcode = newBarcode;
ELSE
SELECT MAX(Barcode) + 1 INTO newBarcode FROM customer;
SET NEW.Barcode = newBarcode;
END IF;
SET NEW.Password = md5(NEW.Password);
END
$$
DELIMITER ;
-- --------------------------------------------------------
--
-- Table structure for table `product`
--
CREATE TABLE `product` (
`ProductID` int(11) NOT NULL,
`Barcode` bigint(13) NOT NULL,
`Name` varchar(30) NOT NULL,
`Price` float NOT NULL,
`StoreQty` int(11) NOT NULL,
`WarehouseQty` int(11) NOT NULL,
`Points` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `product`
--
INSERT INTO `product` (`ProductID`, `Barcode`, `Name`, `Price`, `StoreQty`, `WarehouseQty`, `Points`) VALUES
(1, 8000340479919, 'SKIPPER kiwi mela', 1.5, 10, 40, 15),
(2, 90370762, 'coca cola', 1.5, 10, 38, 10),
(3, 5410041000900, 'TUC crackers', 1.2, 10, 7, 7),
(4, 80007951, 'Acqua san Benedetto', 0.25, 10, 10, 5),
(5, 8010333001850, 'Acqua Martina frizzante', 0.5, 10, 17, 5);
--
-- Triggers `product`
--
DELIMITER $$
CREATE TRIGGER `after_product_insert` AFTER INSERT ON `product` FOR EACH ROW BEGIN
IF NEW.StoreQty < 5 || (NEW.StoreQty >= 5 && NEW.StoreQty < 10) THEN
IF (SELECT COUNT(*) FROM store_order WHERE store_order.ProductID = NEW.ProductID) THEN
UPDATE store_order
SET store_order.Quantity = 10-NEW.StoreQty
WHERE store_order.ProductID = NEW.ProductID;
ELSE
INSERT INTO store_order(ProductID, Quantity) VALUES (NEW.ProductID, 10-NEW.StoreQty);
END IF;
ELSEIF NEW.StoreQty = 10 THEN
IF (SELECT COUNT(*) FROM store_order WHERE store_order.ProductID = NEW.ProductID) THEN
DELETE FROM store_order
WHERE store_order.ProductID = NEW.ProductID;
END IF;
END IF;
IF NEW.WarehouseQty < 10 || (NEW.WarehouseQty >= 10 && NEW.WarehouseQty < 50) THEN
IF (SELECT COUNT(*) FROM warehouse_order WHERE warehouse_order.ProductID = NEW.ProductID) THEN
UPDATE warehouse_order
SET warehouse_order.Quantity = 50-NEW.WarehouseQty
WHERE warehouse_order.ProductID = NEW.ProductID;
ELSE
INSERT INTO warehouse_order(ProductID, Quantity) VALUES (NEW.ProductID, 50-NEW.WarehouseQty);
END IF;
ELSEIF NEW.WarehouseQty = 50 THEN
IF (SELECT COUNT(*) FROM warehouse_order WHERE warehouse_order.ProductID = NEW.ProductID) THEN
DELETE FROM warehouse_order
WHERE warehouse_order.ProductID = NEW.ProductID;
END IF;
END IF;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `after_product_update` AFTER UPDATE ON `product` FOR EACH ROW BEGIN
IF NEW.StoreQty < 5 || (NEW.StoreQty >= 5 && NEW.StoreQty < 10) THEN
IF (SELECT COUNT(*) FROM store_order WHERE store_order.ProductID = NEW.ProductID) THEN
UPDATE store_order
SET store_order.Quantity = 10-NEW.StoreQty
WHERE store_order.ProductID = NEW.ProductID;
ELSE
INSERT INTO store_order(ProductID, Quantity) VALUES (NEW.ProductID, 10-NEW.StoreQty);
END IF;
ELSEIF NEW.StoreQty = 10 THEN
IF (SELECT COUNT(*) FROM store_order WHERE store_order.ProductID = NEW.ProductID) THEN
DELETE FROM store_order
WHERE store_order.ProductID = NEW.ProductID;
END IF;
END IF;
IF NEW.WarehouseQty < 10 || (NEW.WarehouseQty >= 10 && NEW.WarehouseQty < 50) THEN
IF (SELECT COUNT(*) FROM warehouse_order WHERE warehouse_order.ProductID = NEW.ProductID) THEN
UPDATE warehouse_order
SET warehouse_order.Quantity = 50-NEW.WarehouseQty
WHERE warehouse_order.ProductID = NEW.ProductID;
ELSE
INSERT INTO warehouse_order(ProductID, Quantity) VALUES (NEW.ProductID, 50-NEW.WarehouseQty);
END IF;
ELSEIF NEW.WarehouseQty = 50 THEN
IF (SELECT COUNT(*) FROM warehouse_order WHERE warehouse_order.ProductID = NEW.ProductID) THEN
DELETE FROM warehouse_order
WHERE warehouse_order.ProductID = NEW.ProductID;
END IF;
END IF;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `before_product_insert` BEFORE INSERT ON `product` FOR EACH ROW BEGIN
IF NEW.Barcode < 10000000 OR NEW.Barcode > 9999999999999
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add into product: Barcode must be 8 to 13 digits long';
END IF;
IF NEW.Price <= 0.00 || NEW.Price = 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add into product: Price cannot be less than or equal to 0.00';
END IF;
IF NEW.StoreQty < 0 OR NEW.StoreQty > 10
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add into product: Store Quantity must be between 0 and 10';
END IF;
IF NEW.WarehouseQty < 0 OR NEW.WarehouseQty > 50
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add into product: Warehouse Quantity must be between 0 and 50';
END IF;
IF NEW.Points < 0 OR NEW.Points > 100
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add into product: Points must be between 0 and 100';
END IF;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `before_product_update` BEFORE UPDATE ON `product` FOR EACH ROW BEGIN
IF NEW.Barcode < 10000000 OR NEW.Barcode > 9999999999999
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot update product: Barcode must be 8 to 13 digits long';
END IF;
IF NEW.Price <= 0.00 || NEW.Price = 0
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot update product: Price cannot be less than or equal to 0.00';
END IF;
IF NEW.StoreQty < 0 OR NEW.StoreQty > 10
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot update product: Store Quantity must be in between 0 and 10';
END IF;
IF NEW.WarehouseQty < 0 OR NEW.WarehouseQty > 50
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot update product: Warehouse Quantity must be between 0 and 50';
END IF;
IF NEW.Points < 0 OR NEW.Points > 100
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot update product: Points must be between 0 and 100';
END IF;
END
$$
DELIMITER ;
-- --------------------------------------------------------
--
-- Table structure for table `receipt`
--
CREATE TABLE `receipt` (
`ReceiptID` int(11) NOT NULL,
`ProductID` int(11) NOT NULL,
`CustomerID` int(11) NOT NULL,
`Date` date NOT NULL,
`Quantity` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `receipt`
--
INSERT INTO `receipt` (`ReceiptID`, `ProductID`, `CustomerID`, `Date`, `Quantity`) VALUES
(1, 2, 1, '2017-06-23', 2),
(2, 4, 1, '2017-06-23', 3),
(3, 3, 1, '2017-06-18', 1);
--
-- Triggers `receipt`
--
DELIMITER $$
CREATE TRIGGER `after_receipt_insert` AFTER INSERT ON `receipt` FOR EACH ROW BEGIN
UPDATE product
SET product.StoreQty = product.StoreQty - NEW.Quantity
WHERE product.ProductID = NEW.ProductID;
UPDATE customer
SET customer.Points = customer.Points + (SELECT product.Points FROM product WHERE product.ProductID = NEW.ProductID)
WHERE customer.CustomerID = NEW.CustomerID;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `before_receipt_insert` BEFORE INSERT ON `receipt` FOR EACH ROW BEGIN
IF ((SELECT product.StoreQty FROM product WHERE product.ProductID = NEW.ProductID) - NEW.Quantity < 0)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add into receipt: requested quantity is not available';
END IF;
IF NEW.Date > CURRENT_DATE()
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot add into receipt: Invalid Date';
END IF;
END
$$
DELIMITER ;
-- --------------------------------------------------------
--
-- Table structure for table `store_order`
--
CREATE TABLE `store_order` (
`OrderID` int(11) NOT NULL,
`ProductID` int(11) NOT NULL,
`Quantity` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `warehouse_order`
--
CREATE TABLE `warehouse_order` (
`OrderID` int(11) NOT NULL,
`ProductID` int(11) NOT NULL,
`Quantity` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `warehouse_order`
--
INSERT INTO `warehouse_order` (`OrderID`, `ProductID`, `Quantity`) VALUES
(1, 1, 10),
(2, 2, 12),
(3, 3, 43),
(4, 4, 40),
(5, 5, 33);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `customer`
--
ALTER TABLE `customer`
ADD PRIMARY KEY (`CustomerID`);
--
-- Indexes for table `product`
--
ALTER TABLE `product`
ADD PRIMARY KEY (`ProductID`),
ADD UNIQUE KEY `ProductID` (`ProductID`),
ADD UNIQUE KEY `Barcode` (`Barcode`);
--
-- Indexes for table `receipt`
--
ALTER TABLE `receipt`
ADD PRIMARY KEY (`ReceiptID`,`ProductID`,`CustomerID`),
ADD KEY `ProductID` (`ProductID`),
ADD KEY `CustomerID` (`CustomerID`);
--
-- Indexes for table `store_order`
--
ALTER TABLE `store_order`
ADD PRIMARY KEY (`OrderID`),
ADD KEY `ProductID` (`ProductID`);
--
-- Indexes for table `warehouse_order`
--
ALTER TABLE `warehouse_order`
ADD PRIMARY KEY (`OrderID`),
ADD KEY `ProductID` (`ProductID`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `customer`
--
ALTER TABLE `customer`
MODIFY `CustomerID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
--
-- AUTO_INCREMENT for table `product`
--
ALTER TABLE `product`
MODIFY `ProductID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `receipt`
--
ALTER TABLE `receipt`
MODIFY `ReceiptID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `store_order`
--
ALTER TABLE `store_order`
MODIFY `OrderID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `warehouse_order`
--
ALTER TABLE `warehouse_order`
MODIFY `OrderID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `receipt`
--
ALTER TABLE `receipt`
ADD CONSTRAINT `receipt_ibfk_1` FOREIGN KEY (`ProductID`) REFERENCES `product` (`ProductID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `receipt_ibfk_2` FOREIGN KEY (`CustomerID`) REFERENCES `customer` (`CustomerID`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `store_order`
--
ALTER TABLE `store_order`
ADD CONSTRAINT `store_order_ibfk_1` FOREIGN KEY (`ProductID`) REFERENCES `product` (`ProductID`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `warehouse_order`
--
ALTER TABLE `warehouse_order`
ADD CONSTRAINT `warehouse_order_ibfk_1` FOREIGN KEY (`ProductID`) REFERENCES `product` (`ProductID`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;