forked from ricksilva/mysql_cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_12.sql
407 lines (337 loc) · 7.24 KB
/
chapter_12.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
--
-- MySQL Crash Course
--
-- Chapter 12 – Creating Triggers
--
-- You can copy and paste any of these commands into your favorite MySQL tool
-- (like MySQL Workbench) and run them in your own MySQL environment.
--
-- Create the tables for the chapter
create database accounting;
use accounting;
-- Create a table for account payable data for a company
create table payable
(
payable_id int,
company varchar(100),
amount numeric(8,2),
service varchar(100)
);
insert into payable
(
payable_id,
company,
amount,
service
)
values
(1, 'Acme HVAC', 123.32, 'Repair of Air Conditioner'),
(2, 'Initech Printers', 1459.00, 'New Printers'),
(3, 'Hooli Cleaning', 4398.55, 'Janitorial Services');
-- Create the payable_audit table that will track changes to the payable table
create table payable_audit
(
audit_datetime datetime,
audit_user varchar(50),
audit_change varchar(500)
);
-- Create an after insert trigger
drop trigger if exists tr_payable_ai;
delimiter //
create trigger tr_payable_ai
after insert on payable
for each row
begin
insert into payable_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'New row for payable_id ',
new.payable_id,
'. Company: ',
new.company,
'. Amount: ',
new.amount,
'. Service: ',
new.service
)
);
end//
delimiter ;
-- Insert a row into the payable table to test the insert trigger
insert into payable
(
payable_id,
company,
amount,
service
)
values
(
4,
'Sirius Painting',
451.45,
'Painting the lobby'
);
-- Did a row get logged in the payable_audit table showing what was inserted into the payable table?
select * from payable_audit;
-- Create an after delete trigger
use accounting;
drop trigger if exists tr_payable_ad;
delimiter //
create trigger tr_payable_ad
after delete on payable
for each row
begin
insert into payable_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'Deleted row for payable_id ',
old.payable_id,
'. Company: ',
old.company,
'. Amount: ',
old.amount,
'. Service: ',
old.service
)
);
end//
delimiter ;
-- Delete a row from the payable table to test the delete trigger
delete from payable where company = 'Sirius Painting';
-- Is there a row in the payable_audit table that logs the deleted row from the payable table?
select * from payable_audit;
-- Create an after update trigger
delimiter //
create trigger tr_payable_au
after update on payable
for each row
begin
set @change_msg =
concat(
'Updated row for payable_id ',
old.payable_id
);
if (old.company != new.company) then
set @change_msg =
concat(
@change_msg,
'. Company changed from ',
old.company,
' to ',
new.company
);
end if;
if (old.amount != new.amount) then
set @change_msg =
concat(
@change_msg,
'. Amount changed from ',
old.amount,
' to ',
new.amount
);
end if;
if (old.service != new.service) then
set @change_msg =
concat(
@change_msg,
'. Service changed from ',
old.service,
' to ',
new.service
);
end if;
insert into payable_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
@change_msg
);
end//
delimiter ;
-- Test the trigger by updating a row
update payable
set amount = 100000,
company = 'House of Larry'
where payable_id = 3;
-- Did the update get logged?
select * from payable_audit;
create database bank;
use bank;
create table credit
(
customer_id int,
customer_name varchar(100),
credit_score int
);
-- Create a before insert trigger
drop trigger if exists tr_credit_bi;
delimiter //
create trigger tr_credit_bi
before insert on credit
for each row
begin
if (new.credit_score < 300) then
set new.credit_score = 300;
end if;
if (new.credit_score > 850) then
set new.credit_score = 850;
end if;
end//
delimiter ;
-- Test the trigger by inserting some values into the credit table
insert into credit
(
customer_id,
customer_name,
credit_score
)
values
(1, 'Milton Megabucks', 987),
(2, 'Patty Po', 145),
(3, 'Vinny Middle-Class', 702);
-- Create the before delete trigger
use bank;
delimiter //
create trigger tr_credit_bd
before delete on credit
for each row
begin
if (old.credit_score > 750) then
signal sqlstate '45000'
set message_text = 'Cannot delete scores over 750';
end if;
end//
delimiter ;
-- Test the trigger
delete from credit where customer_id = 1;
delete from credit where customer_id = 2;
-- Try it Yourself Exercises
-- Set up for chapter 12 exercises
create database jail;
use jail;
create table alcatraz_prisoner
(
prisoner_id int,
prisoner_name varchar(100)
);
insert into alcatraz_prisoner
(
prisoner_id,
prisoner_name
)
values
(85, 'Al Capone'),
(594, 'Robert Stroud'),
(1476, 'John Anglin');
-- Exercise 12-1: Create the audit table
create table alcatraz_prisoner_audit
(
audit_datetime datetime,
audit_user varchar(100),
audit_change varchar(200)
);
-- Exercise 12-2: Create and test a before insert audit trigger
use jail;
drop trigger if exists tr_alcatraz_prisoner_ai;
-- Create the trigger
delimiter //
create trigger tr_alcatraz_prisoner_ai
after insert on alcatraz_prisoner
for each row
begin
insert into alcatraz_prisoner_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'New row for Prisoner ID ',
new.prisoner_id,
'. Prisoner Name: ',
new.prisoner_name
)
);
end//
delimiter ;
-- Test the trigger by inserting a new prisoner
insert into alcatraz_prisoner
(
prisoner_id,
prisoner_name
)
values
(
117,
'Machine Gun Kelly'
);
-- Did the row get inserted into the alcatraz_prisoner table?
select * from alcatraz_prisoner;
-- Did the new prisoner get logged in the audit table?
select * from alcatraz_prisoner_audit;
-- Set up for exercise 12-3
create database exam;
use exam;
create table grade
(
student_name varchar(100),
score int
);
insert into grade
(
student_name,
score
)
values
('Billy',79),
('Jane', 87),
('Paul', 93);
-- Exercise 12-3: Create the trigger
use exam;
delimiter //
create trigger tr_grade_bu
before update on grade
for each row
begin
if (new.score < 50) then
set new.score = 50;
end if;
if (new.score > 100) then
set new.score = 100;
end if;
end//
delimiter ;
-- Test the trigger by updating some grades
update grade set score = 38 where student_name = 'Billy';
update grade set score = 107 where student_name = 'Jane';
update grade set score = 95 where student_name = 'Paul';
-- Are there no grades lower than 50 or higher than 100?
select * from grade;