-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM3_Database_Triggers_code.sql
52 lines (40 loc) · 1.54 KB
/
M3_Database_Triggers_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
-- M3 db triggers assignment
-- dropping table if it exists
DROP TABLE IF EXISTS t_sup_supplier_hist;
-- creating the table for the history
CREATE TABLE t_sup_supplier_hist (
sup_id varchar(50) NOT NULL,
last_modified date NOT NULL,
PRIMARY KEY (`sup_id`)
);
-- dropping and creating insert trigger
DROP TRIGGER IF EXISTS ins_date;
CREATE TRIGGER ins_date
AFTER INSERT ON t_sup_supplier FOR EACH ROW
INSERT INTO t_sup_supplier_hist
SET sup_id = new.sup_id, last_modified = SYSDATE();
-- dropping and creating delete trigger
DROP TRIGGER IF EXISTS delete_date;
CREATE TRIGGER delete_date
AFTER DELETE ON t_sup_supplier FOR EACH ROW
INSERT INTO t_sup_supplier_hist
(sup_id,last_modified) VALUES (old.sup_id, SYSDATE())
ON DUPLICATE KEY UPDATE last_modified=SYSDATE();
-- dropping and creating update trigger
DROP TRIGGER IF EXISTS update_date;
CREATE TRIGGER update_date
AFTER UPDATE ON t_sup_supplier FOR EACH ROW
INSERT INTO t_sup_supplier_hist (sup_id,last_modified) VALUES (old.sup_id, SYSDATE())
ON DUPLICATE KEY UPDATE last_modified=SYSDATE();
-- testing t_sup_supplier
SELECT * FROM t_sup_supplier
LIMIT 5;
-- inserting values into t_sup_supplier
INSERT INTO t_sup_supplier (sup_id, status_code, country_code, sup_name_en)
VALUES ('2904474', 'val', 'CO', 'ABAX INC.');
-- testing to see if triggers worked
SELECT * FROM t_sup_supplier where sup_id = '2904474';
SELECT * FROM t_sup_supplier_hist;
-- updating t_sup_supplier
UPDATE t_sup_supplier set status_code = 'ini' where sup_id = '2904473';
SELECT * FROM t_sup_supplier_hist;