-
Notifications
You must be signed in to change notification settings - Fork 523
/
10.Triggers and Events.sql
154 lines (114 loc) · 3.75 KB
/
10.Triggers and Events.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
/************************************************/
/* Triggers */
/************************************************/
SELECT * FROM mosh_sql_invoicing.invoices;
SELECT * FROM mosh_sql_invoicing.payments;
/* update total of invoice table, if there is payment added to the payment table */
USE mosh_sql_invoicing;
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;
END $$
DELIMITER ;
/*testing*/
INSERT INTO payments
VALUES(DEFAULT,5,3,'2020-04-05',100,1);
/* trigger that gets fired when we delete the payment */
DROP TRIGGER IF EXISTS payments_after_delete;
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;
END $$
DELIMITER ;
/*testing*/
DELETE FROM payments
WHERE payment_id = 9;
/************************************************/
/* View Triggers */
/************************************************/
SHOW TRIGGERS;
SHOW TRIGGERS LIKE '%payments%';
/************************************************/
/* Drop Triggers */
/************************************************/
DROP TRIGGER IF EXISTS payments_after_delete;
/************************************************/
/* Using Triggers for Auditing */
/************************************************/
USE mosh_sql_invocing;
CREATE TABLE payments_audit(
client_id INT NOT NULL,
date DATE NOT NULL,
amount DECIMAL(9,2) NOT NULL,
action_type VARCHAR(50) NOT NULL,
action_date DATETIME NOT NULL
);
/*trigger for auditing records for insert */
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 ;
/*trigger for auditing records for delete */
DROP TRIGGER IF EXISTS payments_after_delete;
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 ;
/*testing*/
SELECT * FROM payments_audit;
INSERT INTO payments
VALUES(DEFAULT,5,3,'2020-04-05',100,1);
DELETE FROM payments
WHERE payment_id = 10;
/***************************************************/
/* Events */
/* Tasks that get executed according to a scheduel */
/***************************************************/
SHOW VARIABLES LIKE 'event%';
-- usually it is ON, but some organiazion it might be turned off to save resources.
SET GLOBAL event_scheduler = ON;
/* events that delete all the audit recrods which are 1 year old from today */
DELIMITER $$
CREATE EVENT yearly_delete_stale_audit_rows
ON SCHEDULE
EVERY 1 YEAR STARTS '2020-01-01' ENDS '2030-12-31'
DO BEGIN
DELETE FROM payments_audit
WHERE action_date < NOW() - INTERVAL 1 YEAR;
END $$
DELIMITER ;
/***************************************************/
/* View / Alter / Drop Events / Disable / Enable */
/***************************************************/
SHOW EVENTS;
SHOW EVENTS like 'montly%';
DROP EVENT IF EXISTS yearly_delete_stale_audit_rows;
ALTER EVENT yearly_delete_stale_audit_rows DISABLE;
ALTER EVENT yearly_delete_stale_audit_rows ENABLE;