-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic_6_trigger.sql
154 lines (112 loc) · 3.72 KB
/
topic_6_trigger.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
## trigger: a block of SQL code that automatically get executed before or after an insert, update, or delete statement.
## it is often used to enforce data consistency.
# we can modify data in any tables except the payments table (otherwise it will fire itself and lead to infinite loop)
use sql_invoicing;
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;
end $$
delimiter ;
## create another sql script and run:
insert into payments
values (default, 5, 3, '2020-10-10', 10, 1);
## we need to also update the invoice table when delete rows in payment
## so that payment_total is corrected computed. This is cumbersome wonder if better way to keep data consistency.
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 ;
delete
from payments
where payment_id = 14;
show triggers;
show triggers like 'payments%';
############################################ use trigger for auditing table changes: ########################################
# create a new table: payment_audit
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;
# update payment_audit:
insert into payments_audit
values (new.client_id, new.date, new.amount, 'Insert', now());
end $$
delimiter ;
## create another sql script and run:
insert into payments
values (default, 5, 3, '2020-10-10', 10, 1);
## we need to also update the invoice table when delete rows in payment
## so that payment_total is corrected computed. This is cumbersome wonder if better way to keep data consistency.
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;
# update payment_audit:
insert into payments_audit
values (old.client_id, old.date, old.amount, 'Insert', now());
end $$
delimiter ;
## run these to test:
insert into payments
values (default, 5, 2, '2020-10-10', 10, 1);
delete
from payments
where payment_id = 22;
######################################################## events: ###############################################
show variables like 'event%';
set global event_scheduler = on;
set global event_scheduler = off;
delimiter $$
create event yearly_delete_stale_audit_rows
on schedule
-- at 2020-10-24
every 1 year starts '2019-01-01' ends '2029-01-01'
do begin
delete from payment_audit
where action_date < now() - interval 1 year;
#where action_date < dateadd(now(), interval -1 year);
#where action_date < datesub(now(), interval 1 year);
end $$
delimiter ;
###
show events;
show events like 'yearly%';
drop event if exists yearly_delete_stale_audit_rows;
# alter event:
delimiter $$
alter event yearly_delete_stale_audit_rows
on schedule
-- at 2020-10-24
every 1 year starts '2019-01-01' ends '2029-01-01'
do begin
delete from payment_audit
where action_date < now() - interval 1 year;
#where action_date < dateadd(now(), interval -1 year);
#where action_date < datesub(now(), interval 1 year);
end $$
delimiter ;
alter event yearly_delete_stale_audit_rows enable;
alter event yearly_delete_stale_audit_rows disable;