Professional Documents
Culture Documents
Unit 3 Trigger
Unit 3 Trigger
Omprakash Chandrakar
Associate Professor (Computer Science)
2
3
4
5
Bullet Fired
Trigger Pressed
6
Bullet Fired
Trigger Pressed
7
Bullet Fired
Trigger Pressed
8
9
10
11
12
Consider the order table given below. Whenever any order is placed,
the order details is entered into the order table. Now store manager
wants to maintain a log of orders placed with order_id and
log_date_time of the order. What solution would you suggest?
orders
order_log
id customer_id order_date
1 1 2023-03-10
id order_id log_date_time
2 2 2023-03-11
1 1 2023-03-12 12:34:56
3 3 2023-03-11
4 2 2023-03-12 2 2 2023-03-12 12:34:57
4 4 2023-03-12 12:34:59
5 5 2023-03-12 12:35:00
13
DELIMITER //
CREATE TRIGGER order_insert_trigger
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
Trigger Action
INSERT INTO order_log (order_id, log_date_time)
VALUES (NEW.id, NOW());
END//
DELIMITER ;
Trigger Event
INSERT INTO orders (customer_id, order_date) VALUES (1, '2023-03-10');
14
Trigger Name: order_insert_trigger
Triggering Event: Insert | Update | Delete
ON (which table): orders | order_log
Trigger Time: Before | After
Trigger Action: INSERT INTO order_log (order_id, log_date_time)
VALUES (NEW.id, NOW());
15
Trigger Name: order_insert_trigger
Triggering Event: Insert | Update | Delete
ON (which table): orders | order_log
Trigger Time: Before | After
Trigger Action:
16
17
18
Consider the Employee table given below. It contains the record of
employees working in the company. Whenever any employee leaves,
its record is deleted from the Employee table. HR manager wants to
keep the records of ex-employees into ExEmployee table. What
solution you would suggest?
Employee ExEmployee
20
Trigger Name: Trigger1
Triggering Event: Delete
ON (which table): Employee
21
Trigger Name: Trigger1
Triggering Event: Delete
ON (which Employee
table):
Trigger Time: After
Trigger Action: Insert into ExEmployee
values(OLD.Employee_Id, OLD.Name);
005 5 13/03/2023 0
23
Trigger Name: T_Calculate_Bonus_Marks
Triggering Event:
ON (which table):
Trigger Time:
Trigger Action:
24
25
26
27
28
29
30
31
32
Thank You