-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriggers.sql
More file actions
52 lines (37 loc) · 1.17 KB
/
triggers.sql
File metadata and controls
52 lines (37 loc) · 1.17 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
DROP TRIGGER IF EXISTS payments_after_insert;
DELIMITER $$
CREATE TRIGGER payments_after_insert
AFTER INSERT ON payments
FOR EACH ROW
BEGIN
UPDATE invoices
SET payment_total = payment_total + NEW.amount
WHERE invoice_id = NEW.invoice_id;
INSERT INTO payments_audit
VALUES(NEW.client_id, NEW.date, NEW.amount, 'INSERT', NOW());
END$$
DELIMITER ;
SELECT * FROM sql_invoicing.invoices;
INSERT INTO payments
VALUES(DEFAULT, 5, 3, '2019-02-02', 20, 1);
SELECT * FROM sql_invoicing.payments_audit;
SELECT * FROM sql_invoicing.payments;
-- --------------------------------------------- ---------
DROP TRIGGER IF EXISTS payments_after_delete; -- to drop triggers
DELIMITER $$
CREATE TRIGGER payments_after_delete
AFTER DELETE ON payments
FOR EACH ROW
BEGIN
UPDATE invoices
SET payment_total = payment_total - OLD.amount
WHERE invoice_id = OLD.invoice_id;
INSERT INTO payments_audit
VALUES(OLD.client_id, OLD.date, OLD.amount, 'DELETE', NOW());
END $$
DELIMITER ;
SELECT * FROM sql_invoicing.invoices;
DELETE FROM payments
WHERE payment_id = 12;
SELECT * FROM sql_invoicing.payments_audit;
SHOW TRIGGERS; -- to show all triggers in database