-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseProject_SQL_CODE.sql
1218 lines (1019 loc) · 28.9 KB
/
DatabaseProject_SQL_CODE.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-------------------------------------------------DAITO OTAKU TOY SHOP DATABASE--------------------------------------------------------
__________________________________________________________________________________________________________________________________
------------------------------------------------------CREATING TABLES-----------------------------------------------------------------
CREATE database Daito_toy_shop
CREATE TABLE employee
(
empID INT IDENTITY PRIMARY KEY,
empLname VARCHAR(20),
empFname VARCHAR(20),
empAdd VARCHAR(255),
empContactNum VARCHAR(15),
empMail VARCHAR(40) CHECK(empMail LIKE '%@%.%'),
salary FLOAT CHECK(salary > 0),
gender VARCHAR(1) CHECK(gender IN ('M','F','f','m')),
dob DATETIME
);
CREATE TABLE customer
(
custID INT IDENTITY(1,1) PRIMARY KEY,
custFname VARCHAR(35) NOT NULL,
custLname VARCHAR(35) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
custContactNum VARCHAR(15),
custMail VARCHAR(40) CHECK(custMail LIKE '%@%.%') NOT NULL unique
);
CREATE TABLE product
(
productID INT IDENTITY PRIMARY KEY,
prod_desc VARCHAR(255),
stklvl INT CHECK(stklvl > -1),
price FLOAT,--Dollar
product_type VARCHAR(50),
supplierID INT NOT NULL,
);
CREATE TABLE promocode
(
pcode VARCHAR(20) primary key,
deduction FLOAT CHECK(deduction < 1)
);
CREATE table supplier
(
supID INT IDENTITY PRIMARY KEY,
supName VARCHAR(35),
supMail VARCHAR(30) CHECK(supMail LIKE '%@%.%') NOT NULL UNIQUE,
);
CREATE TABLE website
(
loginID INT IDENTITY PRIMARY KEY,
custID INT NOT NULL,
LoginName VARCHAR(35) NOT NULL,
loginPassword VARCHAR(20) NOT NULL
);
CREATE TABLE cart
(
cartId INT IDENTITY PRIMARY KEY,
custId INT NOT NULL,
last_edit_date DATETIME NOT NULL,
shippingID INT,
promocode VARCHAR(20) NULL
);
CREATE TABLE cart_item
(
cartID INT,
productID INT,
Qty INT check(Qty > 0),
primary key(cartID, productID)
);
CREATE TABLE payment
(
paymentId INT IDENTITY PRIMARY KEY,
paydate datetime NOT NULL,
pay_Des VARCHAR(30),
total_Amount FLOAT CHECK(total_Amount > 0) NOT NULL,
cartID INT NOT NULL,
);
CREATE TABLE shipping
(
shippingID INT IDENTITY PRIMARY KEY,
shiptype VARCHAR(20),
deliveryTime INT,
fee FLOAT
);
CREATE TABLE tracking
(
trackingNo INT IDENTITY PRIMARY KEY,
shippingID INT NOT NULL,
paymentID INT NOT NULL
);
ALTER TABLE cart
ADD FOREIGN KEY (custId) REFERENCES customer(custID)
ALTER TABLE cart
ADD FOREIGN KEY (shippingID) REFERENCES shipping(shippingID)
ALTER TABLE cart
add foreign key (promocode) REFERENCES promocode(pcode)
ALTER TABLE product
ADD FOREIGN KEY (supplierID) REFERENCES supplier(supID)
ALTER TABLE cart_item
ADD FOREIGN KEY (cartID) REFERENCES cart(cartID)
ALTER TABLE cart_item
ADD FOREIGN KEY (productID) REFERENCES product(productID)
ALTER TABLE website
ADD FOREIGN KEY (custID) REFERENCES customer(custID)
ALTER TABLE Emp_contact_supplier
ADD FOREIGN KEY (productID) REFERENCES product(productID)
ALTER TABLE payment
ADD FOREIGN KEY (cartID) REFERENCES cart(cartID)
ALTER TABLE tracking
ADD FOREIGN KEY (shippingID) REFERENCES shipping(shippingID)
ALTER TABLE tracking
ADD FOREIGN KEY (paymentID) REFERENCES payment(paymentID)
--------------------------------------------------------------------------------------------------------------------------------------
--Insert Stored Procedures
--1
ALTER PROCEDURE sp_customer_insert
@cust_fname varchar(35), @cust_lname varchar(35), @cust_add varchar(255), @cust_contact_num varchar(15),
@cust_mail varchar(40), @loginname varchar(35) = NULL, @loginpassword varchar(20) = NULL
AS
BEGIN
DECLARE @id integer
set @id = -1
BEGIN TRY
INSERT INTO customer(custFname, custLname, custAddress, custContactNum,custMail) VALUES (@cust_fname, @cust_lname,
@cust_add, @cust_contact_num,@cust_mail)
IF @loginname is not NULL and @loginpassword is not NULL
BEGIN
SELECT @id = MAX(custID)
FROM customer c
END
IF @id <> -1
begin
if not exists(select * from website w where custID = @id)
BEGIN
INSERT INTO website(custID,LoginName,loginPassword) VALUES (@id, @loginname, @loginpassword)
PRINT 'Login details registered successfully !'
END;
ELSE
BEGIN
PRINT 'Customer already have an account, please login to your account'
END;
end
if @id = -1
begin
print 'Visited Customer: details saved !'
end
END TRY
BEGIN CATCH
PRINT 'Error Input'
END CATCH
END
sp_customer_insert 'Gon', 'Uchiha','Flacq','794566133','[email protected]'
sp_customer_insert 'Adams', 'Bryan', 'Curepipe', '7412588962', '[email protected]'
sp_customer_insert 'Jame', 'Donn', 'Curepipe', '745412574685', '[email protected]'
sp_customer_insert 'Franc', 'Frederic', 'Rose Hill', '7412535223', '[email protected]','franc19','87654321'
select *
from customer
select *
from website w2
--2
ALTER PROCEDURE sp_insert_supplier @sup_name VARCHAR(35),@s_mail VARCHAR(30)
AS
BEGIN
BEGIN TRY
INSERT INTO supplier(supName,supMail) VALUES(@sup_name,@s_mail)
END TRY
BEGIN CATCH
PRINT 'Error input !'
END CATCH
END
sp_insert_supplier 'gintama store','[email protected]'
select *
from supplier s
--3
CREATE PROCEDURE sp_insert_promocode @code VARCHAR(20),@reduce FLOAT AS
BEGIN
BEGIN TRY
INSERT INTO promocode (pcode,deduction) VALUES(@code,@reduce)
PRINT 'New promocode added succesfully !'
END TRY
BEGIN CATCH
PRINT 'Error input !'
END CATCH
END
select * from promocode p
sp_insert_promocode 'QWERTY2000',0.25
sp_insert_promocode 'ASDFG2000' ,0.75
--4
ALTER PROCEDURE sp_insert_employee @emp_fname varchar(35), @emp_last varchar(35), @emp_add varchar(255), @emp_contact varchar(15),
@emp_mail varchar(40) = NULL, @emp_salary float = null, @emp_sex varchar(1) = null, @dob datetime = null
AS
BEGIN
DECLARE @id INTEGER
BEGIN TRY
INSERT INTO employee(empFname, empLname, empAdd, empContactNum, empMail,salary,gender,dob)
VALUES(@emp_fname, @emp_last, @emp_add, @emp_contact, @emp_mail, @emp_salary, @emp_sex, @dob);
END TRY
BEGIN CATCH
PRINT 'Error input !'
END catch
END
sp_insert_employee 'Akshit', 'Tooshalduth', 'QuatreBornes', '57890678', '[email protected]', 15000, 'M', '2001-06-11'
sp_insert_employee 'John', 'Bener', 'QuatreBornes', '74185296', '[email protected]', 15000, 'M', '2001-08-11'
select *
from employee e
--5
CREATE Procedure sp_insert_shipping @ship_type VARCHAR(20), @ship_dt INT, @ship_cost FLOAT AS -- dt: delivery time
BEGIN
BEGIN TRY
INSERT INTO shipping(shiptype,deliveryTime ,fee)
VALUES(@ship_type, @ship_dt, @ship_cost)
PRINT 'Shpping method insert successfully !'
END TRY
BEGIN CATCH
PRINT 'Error input !'
END CATCH
END
sp_insert_shipping 'Express Airline', 2, 135.75
select *
from shipping
--6
ALTER PROCEDURE sp_insert_product @prod_desc VARCHAR(255), @stk_lvl INT, @cost FLOAT, @type VARCHAR(50),@sup_ID INT --stk_lvl : Stock Level
AS
BEGIN
DECLARE @id INT
SET @id = @sup_ID
IF NOT EXISTS (SELECT supID
FROM supplier s
WHERE @id = supID)
BEGIN
PRINT 'Supplier ID not found !'
RETURN
END
BEGIN TRY
INSERT INTO product(prod_Desc,stklvl,price,product_type,supplierID)
VALUES (@prod_desc, @stk_lvl, @cost, @type, @sup_ID)
END TRY
BEGIN CATCH
PRINT 'Error input !'
END CATCH
END
sp_insert_product 'Jujutsu', 40, 5.95, 'Statue', 2
sp_insert_product 'Tokyo Ghoul', 100, 16.95, 'Statue', 3
select *
from counttable_products
--7
CREATE PROCEDURE sp_insert_user @cust_id INT,@login_name VARCHAR(35),@login_code VARCHAR(20)
AS
BEGIN
DECLARE @id INT
SET @id = @cust_id
if EXISTS (select * from customer c2 where c2.custID = @id)
BEGIN
print 'account alrealdy exist for this customer, please login'
RETURN
END
IF NOT EXISTS(SELECT custID
FROM customer c
WHERE @id = c.custID)
BEGIN
PRINT 'Error Customer Identification !'
RETURN
END
BEGIN TRY
INSERT INTO website(custID,LoginName,loginPassword) VALUES (@cust_id, @login_name, @login_code)
print 'New user added successfully !'
END TRY
BEGIN CATCH
PRINT 'Error input !'
END CATCH
END
sp_insert_user 6,'KateV','PAsssforwd'
--8#
ALTER PROCEDURE sp_insert_cart @custID INT,@ship_ID INT, @promo_code VARCHAR(20) = NULL
AS
BEGIN
IF NOT EXISTS (SELECT c.custID,s.shippingID
FROM customer c, shipping s
WHERE c.custID = @custID
AND s.shippingID = @ship_ID)
BEGIN
PRINT 'Cart cannot be registered !'
RETURN
END
DECLARE @d DATETIME
SET @d = GETDATE()
BEGIN TRY
INSERT INTO cart(custId, last_edit_date, shippingID, promocode) values(@custID, @d,@ship_ID, @promo_code)
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage
PRINT ERROR_MESSAGE()
END CATCH
END
sp_insert_cart 2, 1, 'QWERTY2000'
--9
ALTER PROCEDURE sp_insert_payment @pay_Desc VARCHAR(30),@cartID INTEGER AS
BEGIN
IF EXISTS(SELECT *
FROM payment p
WHERE @cartID = p.cartId)
BEGIN
print 'Cart already paid !'
return
END
DECLARE @totalfee FLOAT
SET @totalfee = (SELECT SUM (Qty * price)
FROM cart_item ct, product p
WHERE ct.cartID = @cartID
AND ct.productID = p.productID)
DECLARE @d FLOAT -- d: deduction_price
SET @d = (SELECT deduction
FROM promocode p, cart c
WHERE p.pcode = c.promocode and c.cartId = @cartID)
IF @d IS NOT NULL
BEGIN
SET @d = 1 - @d
SET @totalfee = @d * @totalfee
END
DECLARE @shipFee FLOAT
SET @shipFee = (SELECT fee
FROM cart c,shipping s
where c.cartID = @cartID
AND c.shippingID = s.shippingID)
SET @totalfee = @totalfee + @shipFee
BEGIN TRY
INSERT INTO payment
VALUES(getdate(), @pay_Desc, @totalfee, @cartID)
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage
PRINT ERROR_MESSAGE()
END CATCH
END
sp_insert_payment 'CASH', 6
select *
from payment p
select *
from product p
select *
from cart_item ci
--11
CREATE PROCEDURE sp_insert_tracking @pay_ID INT, @ship_ID INT AS
BEGIN
IF NOT EXISTS(SELECT *
FROM payment
WHERE paymentID = @pay_ID)
BEGIN
PRINT 'Payment ID does not exist !'
RETURN
END
IF NOT EXISTS(SELECT *
FROM shipping
WHERE shippingID = @ship_ID)
BEGIN
print 'Shipping ID does not exist !'
END
BEGIN TRY
INSERT INTO tracking
VALUES(@ship_ID,@pay_ID)
PRINT 'New tracking ID assigned succesfully !'
END TRY
BEGIN CATCH
PRINT 'Error input !'
END CATCH
END
sp_insert_tracking 1, 1
--12
ALTER PROCEDURE sp_insert_cartItem @prod_id INT, @cart_ID INT, @Qty INT AS
BEGIN
IF NOT EXISTS (SELECT productID, cartID
FROM product p, cart c
WHERE @prod_id = p.productID
AND @cart_ID = c.cartId)
BEGIN
PRINT 'Input error !'
RETURN
END
DECLARE @stk INT
SET @stk = (SELECT stklvl
FROM product p1
WHERE p1.productID = @prod_id)
IF @stk < @Qty
BEGIN
PRINT 'Not enough in stock !'
RETURN
END
BEGIN TRY
INSERT INTO cart_item(cartID, productID, Qty)
VALUES(@cart_ID,@prod_id, @Qty)
PRINT 'New item added to cart !'
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage
PRINT ERROR_MESSAGE()
END CATCH
END
sp_insert_cartItem 1, 6, 2
sp_insert_cartItem 3, 6, 3
sp_
select *
from cart_item ci
--------------------------------------------------Other Stored Procedures---------------------------------------------------------
--1
CREATE PROCEDURE sp_productQty_totalprice AS --Total price of products for whole stock
BEGIN
SELECT productID, SUM(price * stklvl) AS TOTAL_PRICE
FROM product
GROUP BY productID
END;
EXEC sp_productQty_totalprice;
--2
CREATE PROCEDURE sp_productQty_totalprice_specific_item @id INT AS --Total price of a specific product for whole stock
BEGIN
SELECT SUM(price * stklvl) AS Total_Price
FROM product
WHERE productID = @id
END;
EXEC sp_productQty_totalprice_specific_item 4;
--3
CREATE PROCEDURE sp_chart_total_cost @cID INT AS --total cost in carts
BEGIN
SELECT cartID, SUM(Qty * p.price) AS Total_Cost
FROM product p, cart_item c
WHERE cartID = @cID
AND c.productID = p.productID
GROUP BY c.cartID
END;
EXEC sp_chart_total_cost 4;
--4
CREATE PROCEDURE sp_check_paid AS --Display all that have paid
BEGIN
SELECT c.cartID AS Paid_cart
FROM payment p, cart c
WHERE p.cartID = c.cartID
END;
EXEC sp_check_paid;
--5
CREATE PROCEDURE sp_price_range @low FLOAT = NULL ,@high FLOAT = NULL
AS
BEGIN
SELECT productID, prod_desc, price
FROM product
WHERE price >= @low AND price <= @high
ORDER BY price
END
EXEC sp_price_range 0,30
--6
CREATE PROCEDURE sp_check_active_cart AS --check if a cart is active
BEGIN
SELECT *
FROM cart
WHERE GETDATE() - last_edit_date < 14
--clean cart table of inactive cart
DELETE FROM cart
WHERE GETDATE() - last_edit_date > 14
AND cartID NOT IN (SELECT cartID
FROM payment)
END;
EXEC sp_check_active_cart;
--7
CREATE PROCEDURE sp_stock_price AS--unit price of products
BEGIN
SELECT productID, price
FROM product
END;
EXEC sp_stock_price;
--8
CREATE PROCEDURE sp_in_shop_deliver AS--check which transaction will be taken at the shop
BEGIN
SELECT *
FROM shipping
WHERE fee <= 0
END;
EXEC sp_in_shop_deliver
--9
CREATE PROCEDURE sp_check_need_to_ship
AS
BEGIN
SELECT paymentID
FROM payment
EXCEPT
SELECT p.paymentID
FROM payment p, tracking t
WHERE p.paymentID = t.paymentID
END
EXEC sp_check_need_to_ship
--10
CREATE PROCEDURE sp_highest_sale
AS
BEGIN
SELECT MAX(total_Amount) AS 'Best_Sale $', paydate, c.productID, pr.prod_desc
FROM product pr, payment pa, cart_item c
WHERE pa.cartID = c.cartid
AND c.productID = pr.productID
GROUP BY paymentid, paydate, c.productID, pr.prod_desc
END
EXEC sp_highest_sale
--11
CREATE PROCEDURE sp_stock_price_specific @pid INT AS--unit price of specific product
BEGIN
SELECT productID, price
FROM product
WHERE productID = @pid
END;
EXEC sp_stock_price_specific 2;
--12*************************************************************************
CREATE PROC sp_del_cust @CID INT
AS
BEGIN
BEGIN TRY
DELETE FROM website
WHERE custID = @CID --depends on where customerID is
DELETE FROM customer
WHERE custID = @CID
END TRY
BEGIN CATCH
PRINT 'Error !'
END CATCH
END
sp_del_cust 8
--14
CREATE PROC sp_checks_if_exist
@emp INT = NULL, @cust INT = NULL, @sup INT = NULL, @prod INT = NULL
AS
BEGIN
BEGIN TRY
IF NOT EXISTS (SELECT *
FROM employee
WHERE @emp = empID)
BEGIN
PRINT 'Employee ' + CAST(@emp AS VARCHAR(5)) + ' not found !';
END
ELSE
PRINT 'Employee ' + CAST(@emp AS VARCHAR(5)) + ' found !';
IF NOT EXISTS (SELECT *
FROM customer
WHERE @cust = custID)
BEGIN
PRINT 'Customer ' + CAST(@cust AS VARCHAR(5)) + ' not found !';
END
ELSE
PRINT 'Customer ' + CAST(@cust AS VARCHAR(5)) + ' found !';
IF NOT EXISTS (SELECT *
FROM supplier
WHERE @sup = supID)
BEGIN
PRINT 'Supplier ' + CAST(@sup AS VARCHAR(5)) + ' not found !';
END
ELSE
PRINT 'Supplier ' + CAST(@sup AS VARCHAR(5)) + ' found !';
IF NOT EXISTS (SELECT *
FROM product
WHERE @prod = productID)
BEGIN
PRINT 'Product ' + CAST(@prod AS VARCHAR(5)) + ' not found !';
END
ELSE
PRINT 'Product ' + CAST(@prod AS VARCHAR(5)) + ' found !';
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage
PRINT ERROR_MESSAGE()
END CATCH
END
sp_checks_if_exist 6,4,2,NULL
------------------------------------------------------Triggers------------------------------------------------------------------------
-- 1
ALTER TRIGGER tg_check_customer --check if customer is already added or not
ON customer
INSTEAD OF INSERT
AS
BEGIN
IF EXISTS (SELECT c.custMail
FROM INSERTED i, customer c
WHERE i.custMail = c.custMail)
BEGIN
PRINT 'Mail already in use, retry another or login !'
RETURN
END
declare @cF VARCHAR(35)
declare @cL VARCHAR(35)
declare @cA VARCHAR(255)
declare @cCo VARCHAR(15)
declare @cM VARCHAR(40)
set @cF = (select custFname from inserted)
set @cl = (select custLname from inserted)
set @cA = (select custAddress from inserted)
set @cCo = (select custContactNum from inserted)
set @cM = (select custMail from inserted)
if(select count(custID) from customer) = 0
begin
insert into customer(custID, custFname, custLname,custAddress,custContactNum,custMail) select * from inserted
end
else
begin
declare @cid integer
set @cid = (select MAX(custID)+1 from customer c)
set IDENTITY_INSERT customer ON
insert into customer(custID, custFname, custLname,custAddress,custContactNum,custMail) values(@cid,@cF,@cl,@cA,@cCo,@cM)
set IDENTITY_INSERT customer OFF
end
PRINT 'New customer registered successfully !'
END;
-- 2
ALTER TRIGGER tg_check_email --check email validity
ON employee
INSTEAD OF INSERT
AS
BEGIN
declare @ei integer
declare @el VARCHAR(20)
declare @ef VARCHAR(20)
declare @ea VARCHAR(255)
declare @eC VARCHAR(15)
declare @s FLOAT
declare @g VARCHAR(1)
declare @dob DATETIME
DECLARE @mail AS VARCHAR(30)
set @ei = (select MAX(empID) + 1 from employee e)
set @el = (select empLname from inserted)
set @ef = (select empFname from inserted)
set @ea = (select empAdd from inserted)
set @eC = (select empContactNum from inserted)
set @s = (select salary from inserted)
set @g = (select gender from inserted)
set @dob = (select dob from inserted)
SET @mail = (SELECT empMail FROM INSERTED)
IF @mail not LIKE '%@%.' and EXISTS(select * from inserted i,employee e where e.empMail = i.empMail)
BEGIN
PRINT 'Invalid email.'
RETURN
END
if @ei = null
begin
set @ei = 0
end
set IDENTITY_INSERT employee on
insert into employee(empID,empFname, empLname, empAdd, empContactNum, empMail,salary,gender,dob) values(@ei,@el,@ef,@ea,@eC,@mail,@s,@g,@dob)
set IDENTITY_INSERT employee off
PRINT 'New employee saved successfully !'
END;
-- 3
ALTER TRIGGER tg_check_product --check if product is already added or not
ON product
INSTEAD OF INSERT
AS
BEGIN
DECLARE @quantityinstock INT
SET @quantityinstock = (SELECT stklvl FROM inserted)
DECLARE @prodid INT
SET @prodid = (SELECT MAX(productID) + 1 FROM product p)
declare @d varchar(255)
declare @t varchar(50)
declare @s integer
declare @p float
set @p = (select price from inserted)
set @d = (select prod_desc from inserted)
set @t = (select product_type from inserted)
set @s = (select supplierID from inserted)
if @prodid = null
begin
set @prodid = 0
end
IF EXISTS (SELECT * FROM INSERTED i, product p WHERE i.prod_desc = p.prod_desc)
BEGIN
PRINT 'Product already exists !'
UPDATE product
SET stklvl = stklvl + @quantityinstock
WHERE @d = prod_desc
END
ELSE
BEGIN
set IDENTITY_INSERT product on
insert into product(ProductID, prod_desc,stklvl,price,product_type,supplierID) values (@prodid,@d,@quantityinstock,@p,@t,@s)
set IDENTITY_INSERT product off
PRINT 'Product insert successfully !'
END
END;
-- 4
CREATE TRIGGER tg_increase_count_products --increment product count
ON product
AFTER INSERT
AS
BEGIN
UPDATE counttable_products
SET Number_of_Products = Number_of_Products + 1;
END;
CREATE TABLE counttable_products
(
Number_of_Products INT
);
INSERT INTO counttable_products VALUES(0)
--5
CREATE TRIGGER tg_check_supplier--check if supplier is already added or not
ON supplier
INSTEAD OF INSERT
AS
BEGIN
IF EXISTS (SELECT *
FROM INSERTED i, supplier s
WHERE i.supName = s.supName)
BEGIN
PRINT 'Supplier already exists.'
RETURN
END
declare @supID integer
declare @supMail varchar(30)
declare @supName varchar(36)
set @supID = (select max(supID) from supplier)
set @supMail = (select supMail from INSERTED)
set @supName= (select supName from INSERTED)
if @supID = null
begin
set @supID = 0
end
set IDENTITY_INSERT supplier on
insert into supplier(supID,supName,supMail) values(@supID,@supName,@supMail)
set IDENTITY_INSERT supplier OFF
PRINT 'New supplier added successfully !'
END;
--6
CREATE TRIGGER tg_disp_prod_details
ON product
AFTER INSERT
AS
BEGIN
DECLARE @id AS INTEGER;
DECLARE @description AS VARCHAR(20);
DECLARE @stklvl AS INT;
DECLARE @type AS VARCHAR(20);
DECLARE @supplier AS VARCHAR(5);
SET @id = (SELECT productID FROM inserted);
SET @description = (SELECT prod_desc FROM inserted);
SET @stklvl = (SELECT stklvl FROM inserted);
SET @type = (SELECT product_type FROM inserted);
SET @supplier = (SELECT supplierID FROM inserted);
PRINT 'New product ' + @description ;
END;
--7
CREATE TRIGGER tg_store_old_data
ON product
AFTER UPDATE
AS
BEGIN
DECLARE @old_price AS REAL;
SET @old_price = (SELECT price FROM DELETED);
INSERT INTO price_audit_product (ID, ProdDesc, old_price, new_price)
SELECT productID, prod_desc, @old_price, price FROM INSERTED
END;
CREATE TABLE price_audit_product
(
ID INTEGER PRIMARY KEY,
ProdDesc VARCHAR(30) NOT NULL,
old_price REAL,
new_price REAL,
);
UPDATE product
SET price = 99.99
WHERE productID = 4
SELECT *
FROM product
select *
from price_audit_product
--8
CREATE TRIGGER tg_old_products
ON product
AFTER DELETE
AS
BEGIN
IF @@ROWCOUNT = 0
RETURN
DECLARE @id AS INTEGER;
DECLARE @desc AS VARCHAR(40);
SET @id = (SELECT productID FROM DELETED);
SET @desc = (SELECT prod_desc FROM DELETED);
INSERT INTO products_not_in_sale_anymore VALUES(@id, @desc, GETDATE());
END;
CREATE TABLE products_not_in_sale_anymore
(
pid INTEGER NOT NULL,
pdesc VARCHAR(40),
date_deleted DATETIME,
CONSTRAINT book_old_pkey PRIMARY KEY(pid, date_deleted),
CONSTRAINT date_dlt_isbn_key UNIQUE(pdesc)
);
SELECT * FROM products_not_in_sale_anymore
DELETE FROM product
WHERE productID = 11
--9
ALTER TRIGGER tg_updte_stk
ON payment
AFTER INSERT
AS
BEGIN
UPDATE product
SET stklvl = stklvl - Qty
FROM cart_item, inserted i
WHERE cart_item.cartID = i.cartID
AND product.productID = cart_item.productID
declare @cart integer
set @cart = (select cartID from inserted)
delete from cart_item
where productID in(
select ci.productID
from cart_item ci, product p
WHERE ci.productID in (
select productID
from cart_item ci, inserted i
where i.cartID = ci.cartID)
and p.stklvl < ci.Qty)
and cartID <> @cart
END
-- 10
CREATE TRIGGER tg_count_customer