-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComp_Data_1_Part8
More file actions
55 lines (44 loc) · 1.22 KB
/
Comp_Data_1_Part8
File metadata and controls
55 lines (44 loc) · 1.22 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
50
51
52
53
54
55
Hello, we will use Triggers in this file. Let's see.
-- CREATE
-- TRIGGER `event_name` BEFORE/AFTER INSERT/UPDATE/DELETE
-- ON `database`.`table`
-- FOR EACH ROW BEGIN
-- -- trigger body
-- -- this code is applied to every
-- -- inserted/updated/deleted row
-- END;
####
CREATE TABLE trigger_test (
message VARCHAR(100)
);
#### I ran the following commands on MySQL Command Line Client-----
####
DELIMITER $$
CREATE
TRIGGER my_trigger BEFORE INSERT
ON employee
FOR EACH ROW BEGIN
INSERT INTO trigger_test VALUES('added new employee');
END$$
DELIMITER ;
Then on PopSQL,
INSERT INTO employee
VALUES(109, 'Oscar', 'Martinez', '1968-02-19', 'M', 69000, 106, 3);
##
DELIMITER $$
CREATE
TRIGGER my_trigger BEFORE INSERT
ON employee
FOR EACH ROW BEGIN
IF NEW.sex = 'M' THEN
INSERT INTO trigger_test VALUES('added male employee');
ELSEIF NEW.sex = 'F' THEN
INSERT INTO trigger_test VALUES('added female');
ELSE
INSERT INTO trigger_test VALUES('added other employee');
END IF;
END$$
DELIMITER ;
Then on PopSQL,
INSERT INTO employee
VALUES(111, 'Pam', 'Beesly', '1988-02-19', 'F', 69000, 106, 3);