forked from ricksilva/mysql_cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_17.sql
923 lines (746 loc) · 25.3 KB
/
chapter_17.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
--
-- MySQL Crash Course
--
-- Chapter 17 – Using Triggers to Prevent Errors and Track Changes to Voter Data
--
-- 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.
--
-- To find answers to particular exercises, search through this file for the exercise number, like "17-5"
-- Answers to exercises aren't all in lowest-to-highest order in this script.
-- Exercise 17-1 is to create the voting database and tables:
drop database if exists voting;
create database voting;
use voting;
-- Creating the data tables
create table voter
(
voter_id int primary key auto_increment, -- automatically inserts a voter_id value (answers exercise 17-3)
voter_name varchar(100) not null,
voter_address varchar(100) not null,
voter_county varchar(50) not null,
voter_district varchar(10) not null,
voter_precinct varchar(10) not null,
voter_party varchar(20),
voting_location varchar(100) not null,
voter_registration_num int not null unique
);
create table ballot
(
ballot_id int primary key auto_increment,
voter_id int not null unique,
ballot_type varchar(10) not null,
ballot_cast_datetime datetime not null,
constraint foreign key (voter_id) references voter(voter_id),
constraint check(ballot_type in ('in-person', 'absentee'))
);
create table race
(
race_id int primary key auto_increment,
race_name varchar(100) not null unique,
votes_allowed int not null
);
create table candidate
(
candidate_id int primary key auto_increment,
race_id int not null,
candidate_name varchar(100) not null unique,
candidate_address varchar(100) not null,
candidate_party varchar(20),
incumbent_flag bool,
constraint foreign key (race_id) references race(race_id)
);
create table ballot_candidate
(
ballot_id int,
candidate_id int,
primary key (ballot_id, candidate_id),
constraint foreign key (ballot_id) references ballot(ballot_id),
constraint foreign key (candidate_id) references candidate(candidate_id)
);
-- End of Exercise 17-1
-- Creating the audit tables
create table voter_audit
(
audit_datetime datetime,
audit_user varchar(100),
audit_change varchar(1000)
);
create table ballot_audit
(
audit_datetime datetime,
audit_user varchar(100),
audit_change varchar(1000)
);
create table race_audit
(
audit_datetime datetime,
audit_user varchar(100),
audit_change varchar(1000)
);
create table candidate_audit
(
audit_datetime datetime,
audit_user varchar(100),
audit_change varchar(1000)
);
create table ballot_candidate_audit
(
audit_datetime datetime,
audit_user varchar(100),
audit_change varchar(1000)
);
-- Creating the "after insert" triggers
-- tr_voter_ai
drop trigger if exists tr_voter_ai;
delimiter //
create trigger tr_voter_ai
after insert on voter
for each row
begin
insert into voter_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'New voter added -',
' voter_id: ', new.voter_id,
' voter_name: ', new.voter_name,
' voter_address: ', new.voter_address,
' voter_county: ', new.voter_county,
' voter_district: ', new.voter_district,
' voter_precinct: ', new.voter_precinct,
' voter_party: ', new.voter_party,
' voting_location: ', new.voting_location,
' voter_registration_num: ', new.voter_registration_num
)
);
end//
delimiter ;
-- Exercise 17-8
drop trigger if exists tr_ballot_ai;
delimiter //
create trigger tr_ballot_ai
after insert on ballot
for each row
begin
insert into ballot_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'New ballot added -',
' ballot_id ', new.ballot_id,
' voter_id: ', new.voter_id,
' ballot_type: ', new.ballot_type,
' ballot_cast_datetime: ', new.ballot_cast_datetime
)
);
end//
delimiter ;
-- End of Exercise 17-8
-- Exercise 17-9
-- tr_race_ai
drop trigger if exists tr_race_ai;
delimiter //
create trigger tr_race_id
after insert on race
for each row
begin
insert into race_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'New race added -',
' race_id ', new.race_id,
' race_name: ', new.race_name
)
);
end//
delimiter ;
-- tr_candidate_ai
drop trigger if exists tr_candidate_ai;
delimiter //
create trigger tr_candidate_ai
after insert on candidate
for each row
begin
insert into candidate_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'New candidate added -',
' candidate_id ', new.candidate_id,
' race_id: ', new.race_id,
' candidate_name: ', new.candidate_name,
' candidate_address: ', new.candidate_address,
' candidate_party: ', new.candidate_party,
' incumbent_flag: ', new.incumbent_flag
)
);
end//
delimiter ;
-- tr_ballot_candidate_ai
drop trigger if exists tr_ballot_candidate_ai;
delimiter //
create trigger tr_ballot_candidate_ai
after insert on ballot_candidate
for each row
begin
insert into ballot_candidate_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'New ballot_candidate row added -',
' ballot_id ', new.ballot_id,
' candidate_id: ', new.candidate_id
)
);
end//
delimiter ;
-- End of Exercise 17-9
-- Create the after delete triggers
-- tr_voter_ad
drop trigger if exists tr_voter_ad;
delimiter //
create trigger tr_voter_ad
after delete on voter
for each row
begin
insert into voter_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'voter deleted -',
' voter_id: ', old.voter_id,
' voter_name: ', old.voter_name,
' voter_address: ', old.voter_address,
' voter_county: ', old.voter_county,
' voter_district: ', old.voter_district,
' voter_precinct: ', old.voter_precinct,
' voter_party: ', old.voter_party,
' voting_location: ', old.voting_location,
' voter_registration_num: ', old.voter_registration_num
)
);
end//
delimiter ;
-- Exercise 17-10:
-- tr_ballot_ad
drop trigger if exists tr_ballot_ad;
delimiter //
create trigger tr_ballot_ad
after delete on ballot
for each row
begin
insert into ballot_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'ballot deleted -',
' ballot_id: ', old.ballot_id,
' voter_id: ', old.voter_id,
' ballot_type: ', old.ballot_type,
' ballot_cast_datetime: ', old.ballot_cast_datetime
)
);
end//
delimiter ;
-- End of Exercise 17-10
-- Exercise 17-11:
-- tr_race_ad
drop trigger if exists tr_race_ad;
delimiter //
create trigger tr_race_ad
after delete on race
for each row
begin
insert into race_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'race deleted -',
' race_id: ', old.race_id,
' race_name: ', old.race_name
)
);
end//
delimiter ;
-- tr_candidate_ad
drop trigger if exists tr_candidate_ad;
delimiter //
create trigger tr_candidate_ad
after delete on candidate
for each row
begin
insert into candidate_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'candidate deleted -',
' candidate_id: ', old.candidate_id,
' race_id: ', old.race_id,
' candidate_name: ', old.candidate_name,
' candidate_address: ', old.candidate_address,
' candidate_party: ', old.candidate_party,
' incumbent_flag: ', old.incumbent_flag
)
);
end//
delimiter ;
-- tr_ballot_candidate_ad
drop trigger if exists tr_ballot_candidate_ad;
delimiter //
create trigger tr_ballot_candidate_ad
after delete on ballot_candidate
for each row
begin
insert into ballot_candidate_audit
(
audit_datetime,
audit_user,
audit_change
)
values
(
now(),
user(),
concat(
'ballot_candidate row deleted -',
' ballot_id: ', old.ballot_id,
' candidate_id: ', old.candidate_id
)
);
end//
delimiter ;
-- End of Exercise 17-11
-- Create the "after update" triggers
-- tr_voter_au
drop trigger if exists tr_voter_au;
delimiter //
create trigger tr_voter_au
after update on voter
for each row
begin
set @change_msg = concat('voter ',old.voter_id,' updated');
if new.voter_id != old.voter_id then
set @change_msg = concat(@change_msg, concat('. voter_id changed from ', old.voter_id, ' to ', new.voter_id));
end if;
if new.voter_name != old.voter_name then
set @change_msg = concat(@change_msg, concat('. voter_name changed from ', old.voter_name, ' to ', new.voter_name));
end if;
if new.voter_address != old.voter_address then
set @change_msg = concat(@change_msg, concat('. voter_address changed from ', old.voter_address, ' to ', new.voter_address));
end if;
if new.voter_county != old.voter_county then
set @change_msg = concat(@change_msg, concat('. voter_county changed from ', old.voter_county, ' to ', new.voter_county));
end if;
if new.voter_district != old.voter_district then
set @change_msg = concat(@change_msg, concat('. voter_district changed from ', old.voter_district, ' to ', new.voter_district));
end if;
if new.voter_precinct != old.voter_precinct then
set @change_msg = concat(@change_msg, concat('. voter_precinct changed from ', old.voter_precinct, ' to ', new.voter_precinct));
end if;
if new.voter_party != old.voter_party then
set @change_msg = concat(@change_msg, concat('. voter_party changed from ', old.voter_party, ' to ', new.voter_party));
end if;
if new.voting_location != old.voting_location then
set @change_msg = concat(@change_msg, concat('. voting_location changed from ', old.voting_location, ' to ', new.voting_location));
end if;
if new.voter_registration_num != old.voter_registration_num then
set @change_msg = concat(@change_msg, concat('. voter_registration changed from ', old.voter_registration_num, ' to ', new.voter_registration_num));
end if;
insert into voter_audit(audit_datetime, audit_user, audit_change)
values (now(), user(), @change_msg);
end//
delimiter ;
-- Exercise 17-12:
-- tr_ballot_au
drop trigger if exists tr_ballot_au;
delimiter //
create trigger tr_ballot_au
after update on ballot
for each row
begin
set @change_msg = concat('ballot ',old.ballot_id,' updated');
if new.ballot_id != old.ballot_id then
set @change_msg = concat(@change_msg, concat('. ballot_id changed from ', old.ballot_id, ' to ', new.ballot_id));
end if;
if new.voter_id != old.voter_id then
set @change_msg = concat(@change_msg, concat('. voter_id changed from ', old.voter_id, ' to ', new.voter_id));
end if;
if new.ballot_type != old.ballot_type then
set @change_msg = concat(@change_msg, concat('. ballot_type changed from ', old.ballot_type, ' to ', new.ballot_type));
end if;
if new.ballot_cast_datetime != old.ballot_cast_datetime then
set @change_msg = concat(@change_msg, concat('. ballot_cast_datetime changed from ', old.ballot_cast_datetime, ' to ', new.ballot_cast_datetime));
end if;
insert into ballot_audit(audit_datetime, audit_user, audit_change)
values (now(), user(), @change_msg);
end//
delimiter ;
-- End of Exercise 17-12
-- Exercise 17-13:
-- tr_race_au
drop trigger if exists tr_race_au;
delimiter //
create trigger tr_race_au
after update on race
for each row
begin
set @change_msg = concat('race ',old.race_id,' updated');
if new.race_id != old.race_id then
set @change_msg = concat(@change_msg, concat('. race_id changed from ', old.race_id, ' to ', new.race_id));
end if;
if new.race_name != old.race_name then
set @change_msg = concat(@change_msg, concat('. race_name changed from ', old.race_name, ' to ', new.race_name));
end if;
insert into race_audit(audit_datetime, audit_user, audit_change)
values (now(), user(), @change_msg);
end//
delimiter ;
-- tr_candidate_au
drop trigger if exists tr_candidate_au;
delimiter //
create trigger tr_candidate_au
after update on candidate
for each row
begin
set @change_msg = concat('candidate ',old.candidate_id,' updated');
if new.candidate_id != old.candidate_id then
set @change_msg = concat(@change_msg, concat('. candidate_id changed from ', old.candidate_id, ' to ', new.candidate_id));
end if;
if new.race_id != old.race_id then
set @change_msg = concat(@change_msg, concat('. race_id changed from ', old.race_id, ' to ', new.race_id));
end if;
if new.candidate_name != old.candidate_name then
set @change_msg = concat(@change_msg, concat('. candidate_name changed from ', old.candidate_name, ' to ', new.candidate_name));
end if;
if new.candidate_address != old.candidate_address then
set @change_msg = concat(@change_msg, concat('. candidate_address changed from ', old.candidate_address, ' to ', new.candidate_address));
end if;
if new.candidate_party != old.candidate_party then
set @change_msg = concat(@change_msg, concat('. candidate_party changed from ', old.candidate_party, ' to ', new.candidate_party));
end if;
if new.incumbent_flag != old.incumbent_flag then
set @change_msg = concat(@change_msg, concat('. incumbent_flag changed from ', old.incumbent_flag, ' to ', new.incumbent_flag));
end if;
insert into candidate_audit(audit_datetime, audit_user, audit_change)
values (now(), user(), @change_msg);
end//
delimiter ;
-- tr_ballot_candidate_au
drop trigger if exists tr_ballot_candidate_au;
delimiter //
create trigger tr_ballot_candidate_au
after update on ballot_candidate
for each row
begin
set @change_msg = concat('ballot_candidate changed for ballot_id ',old.ballot_id,', candidate_id ',old.candidate_id);
if new.ballot_id != old.ballot_id then
set @change_msg = concat(@change_msg, concat('. ballot_id changed from ', old.ballot_id, ' to ', new.ballot_id));
end if;
if new.candidate_id != old.candidate_id then
set @change_msg = concat(@change_msg, concat('. candidate_id changed from ', old.candidate_id, ' to ', new.candidate_id));
end if;
insert into ballot_candidate_audit(audit_datetime, audit_user, audit_change)
values (now(), user(), @change_msg);
end//
delimiter ;
-- End exercise 17-13
-- Create the ballot_candidate before insert trigger tr_ballot_candidate_bi to prevent overvoting
drop trigger if exists tr_ballot_candidate_bi;
delimiter //
create trigger tr_ballot_candidate_bi
before insert on ballot_candidate
for each row
begin
declare v_race_id int;
declare v_votes_allowed int;
declare v_existing_votes int;
declare v_error_msg varchar(100);
declare v_race_name varchar(100);
select r.race_id,
r.race_name,
r.votes_allowed
into v_race_id,
v_race_name,
v_votes_allowed
from race r
join candidate c
on r.race_id = c.race_id
where c.candidate_id = new.candidate_id;
select count(*)
into v_existing_votes
from ballot_candidate bc
join candidate c
on bc.candidate_id = c.candidate_id
and c.race_id = v_race_id
where bc.ballot_id = new.ballot_id;
if v_existing_votes >= v_votes_allowed then
select concat('Overvoting error: The ',
v_race_name,
' race allows selecting a maximum of ',
v_votes_allowed,
' candidate(s) per ballot.'
)
into v_error_msg;
signal sqlstate '45000' set message_text = v_error_msg;
end if;
end//
delimiter ;
-- Insert data into tables
-- voter table
-- This first insert statement to the voter table is Exercise 17-2
insert into voter (voter_name, voter_address, voter_county, voter_district, voter_precinct, voter_party, voting_location, voter_registration_num)
values ('Susan King', '12 Pleasant St. Springfield', 'Franklin', '12A', '4C', 'Democrat', '523 Emerson St.', 129756);
-- End of Exercise 17-2
-- Answer to Exercise 17-3: The voter_id column value gets automatically added to the table because
-- voter_id was defined as "auto increment" when we created the voter table.
-- MySQL will automatically make sure the value of voter_id gets higher with each new row.
insert into voter (voter_name, voter_address, voter_county, voter_district, voter_precinct, voter_party, voting_location, voter_registration_num)
values ('Tina Warren', '218 Elm St. Manchester', 'Franklin', '12A', '4C', 'Republican', '523 Emerson St.', 698763);
insert into voter (voter_name, voter_address, voter_county, voter_district, voter_precinct, voter_party, voting_location, voter_registration_num)
values ('Luke Smith', '87 Main St. Madison', 'Franklin', '12A', '4C', 'Unenrolled', '523 Emerson St.', 3859124);
-- race table
insert into race (race_name, votes_allowed) values ('Mayor', 1);
insert into race (race_name, votes_allowed) values ('Board of Health', 1);
insert into race (race_name, votes_allowed) values ('Treasurer', 1);
insert into race (race_name, votes_allowed) values ('School Committee', 2);
insert into race (race_name, votes_allowed) values ('Planning Board', 1);
-- candidate table
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (1, 'Lawrence Q. Mow', '1 Prestigous Way', 'Republican', 1);
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (1, 'Maria Dolan', '11 Cove St', 'Democrat', 0);
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (2, 'Lily Turner', '88 Flanders Ln', 'Democrat', 1);
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (2, 'Ruby Clark', '12 Oak St', 'Independent', 0);
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (3, 'Liza Warbucks', '5 Lincoln Ave', 'Democrat', 1);
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (3, 'William Banks', '63 Brewster St', 'Republican', 0);
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (3, 'Andrew T. Oates', '230 Tremont Pl', null, 0);
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (4, 'Elaine M. Gold', '67 Fairbanks St', 'Republican', 1);
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (4, 'Sarah V. Hall', '7 Harrison St', 'Democrat', 0);
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (4, 'Peter Smart', '16 Wayne Rd', 'Democrat', 0);
insert into candidate (race_id, candidate_name, candidate_address, candidate_party, incumbent_flag)
values (5, 'Michael J. Hogan', '2 Pine Hill Rd', 'Green', 1);
-- ballot table
insert into ballot (voter_id, ballot_type, ballot_cast_datetime)
values (1, 'in-person', '2024-04-26 14:47:29');
-- Exercise 17-4: To run these insert statements, uncomment the insert statements (remove the --'s) and run them.
-- They should fail because they violate the constraints we set up when we created our tables.
--
-- The following insert statement will fail because the voter table already has a voter with voter_registration_num 129756:
--
-- insert into voter (voter_name, voter_address, voter_county, voter_district, voter_precinct, voter_party, voting_location, voter_registration_num)
-- values ('Ed Hart', '7 Church St. Waverly', 'Franklin', '12A', '4C', 'Republican', '523 Emerson St.', 129756);
--
-- The following insert statement will fail because we don't have a voter with a voter_id of 888 in the voter table
--
-- insert into ballot (voter_id, ballot_type, ballot_cast_datetime)
-- values (888, 'in-person', now());
--
-- End of Exercise 17-4
-- ballot_candidate table
insert into ballot_candidate (ballot_id, candidate_id)
values (1, 2);
insert into ballot_candidate (ballot_id, candidate_id)
values (1, 8);
-- Create the triggers that allow only the Secretary of State to make changes to voter, race, and candidate data from now on.
-- Creating the tr_voter_bi before insert trigger
drop trigger if exists tr_voter_bi;
delimiter //
create trigger tr_voter_bi
before insert on voter
for each row
begin
if user() not like 'secretary_of_state%' then
signal sqlstate '45000'
set message_text = 'Voters can be added only by the Secretary of State';
end if;
end//
delimiter ;
-- Exercise 17-5
-- Creating the tr_race_bi before insert trigger
drop trigger if exists tr_race_bi;
delimiter //
create trigger tr_race_bi
before insert on race
for each row
begin
if user() not like 'secretary_of_state%' then
signal sqlstate '45000'
set message_text = 'Races can be added only by the Secretary of State';
end if;
end//
delimiter ;
-- Creating the tr_candidate_bi before insert trigger
drop trigger if exists tr_candidate_bi;
delimiter //
create trigger tr_candidate_bi
before insert on candidate
for each row
begin
if user() not like 'secretary_of_state%' then
signal sqlstate '45000'
set message_text = 'Candidates can be added only by the Secretary of State';
end if;
end//
delimiter ;
-- To test these "before" triggers on the voter, candidate, and race tables, create a new user called secretary_of_state, like this:
-- create user secretary_of_state@localhost identified by 'v0t3'; -- This creates the user's password as v0t3
-- grant all privileges on *.* to secretary_of_state@localhost; -- granting superuser privs on everything is normally a bad idea, but we can do it just for this test.
-- If you log into MySQL as the Secretary of State you can execute these 3 commands (uncomment the commands first)
-- If you log into MySQL with another user ID, our triggers will prevent running the commands.
-- insert into race (race_name, votes_allowed)
-- values ('Dog Catcher', 1);
-- End of Exercise 17-5
-- Creating the tr_voter_bu before update trigger
drop trigger if exists tr_voter_bu;
delimiter //
create trigger tr_voter_bu
before update on voter
for each row
begin
if user() not like 'secretary_of_state%' then
signal sqlstate '45000' set message_text = 'Voters can be updated only by the Secretary of State';
end if;
end//
delimiter ;
-- Exercise 17-6
-- Creating the tr_candidate_bu before update trigger
drop trigger if exists tr_candidate_bu;
delimiter //
create trigger tr_candidate_bu
before update on candidate
for each row
begin
if user() not like 'secretary_of_state%' then
signal sqlstate '45000' set message_text = 'Candidates can be updated only by the Secretary of State';
end if;
end//
delimiter ;
-- Creating the tr_race_bu before update trigger
drop trigger if exists tr_race_bu;
delimiter //
create trigger tr_race_bu
before update on race
for each row
begin
if user() not like 'secretary_of_state%' then
signal sqlstate '45000' set message_text = 'Races can be updated only by the Secretary of State';
end if;
end//
delimiter ;
-- Exercise 17-6
-- Creating the tr_voter_bd before delete trigger
drop trigger if exists tr_voter_bd;
delimiter //
create trigger tr_voter_bd
before delete on voter
for each row
begin
if user() not like 'secretary_of_state%' then
signal sqlstate '45000' set message_text = 'Voters can be deleted only by the Secretary of State';
end if;
end//
delimiter ;
-- Exercise 17-7
-- Creating the tr_candidate_bd before delete trigger
drop trigger if exists tr_candidate_bd;
delimiter //
create trigger tr_candidate_bd
before delete on candidate
for each row
begin
if user() not like 'secretary_of_state%' then
signal sqlstate '45000' set message_text = 'Candidates can be deleted only by the Secretary of State';
end if;
end//
delimiter ;
-- Creating the tr_race_bd before delete trigger
drop trigger if exists tr_race_bd;
delimiter //
create trigger tr_race_bd
before delete on race
for each row
begin
if user() not like 'secretary_of_state%' then
signal sqlstate '45000' set message_text = 'Races can be deleted only by the Secretary of State';
end if;
end//
delimiter ;
-- End of exercise 17-7
-- update candidate
-- set candidate_name = 'Lisa Peacemoney'
-- where candidate_name = 'Liza Warbucks';
-- delete from voter
-- where voter_name = 'Tina Warren';