1-- The following statement creates an AFTER UPDATE trigger on the sales table:
2
3DELIMITER $$
4
5CREATE TRIGGER after_sales_update
6AFTER UPDATE
7ON sales FOR EACH ROW
8BEGIN
9 IF OLD.quantity <> new.quantity THEN
10 INSERT INTO SalesChanges(salesId,beforeQuantity, afterQuantity)
11 VALUES(old.id, old.quantity, new.quantity);
12 END IF;
13END$$
14
15DELIMITER ;
1CREATE
2 TRIGGER `event_name` BEFORE/AFTER INSERT/UPDATE/DELETE
3 ON `database`.`table`
4 FOR EACH ROW BEGIN
5 -- trigger body
6 -- this code is applied to every
7 -- inserted/updated/deleted row
8 END;
9
10CREATE
11 TRIGGER `blog_after_insert` AFTER INSERT
12 ON `blog`
13 FOR EACH ROW BEGIN
14
15 IF NEW.deleted THEN
16 SET @changetype = 'DELETE';
17 ELSE
18 SET @changetype = 'NEW';
19 END IF;
20
21 INSERT INTO audit (blog_id, changetype) VALUES (NEW.id, @changetype);
22
23 END$$
1CREATE TRIGGER trigger_name
2{BEFORE | AFTER} {INSERT | UPDATE| DELETE }
3ON table_name FOR EACH ROW
4trigger_body;
1CREATE TRIGGER Product_Details_tr
2BEFORE INSERT ON Product_Details
3FOR EACH ROW
4SET NEW.User_ID = CURRENT_USER();