1
2
3
4
5 CREATE TRIGGER trigger_name
6 BEFORE INSERT
7 ON table_name FOR EACH ROW
8trigger_body;
9
1
2
3
4
5 DELIMITER $$
6
7CREATE TRIGGER before_workcenters_insert
8BEFORE INSERT
9ON WorkCenters FOR EACH ROW
10BEGIN
11 DECLARE rowcount INT;
12
13 SELECT COUNT(*)
14 INTO rowcount
15 FROM WorkCenterStats;
16
17 IF rowcount > 0 THEN
18 UPDATE WorkCenterStats
19 SET totalCapacity = totalCapacity + new.capacity;
20 ELSE
21 INSERT INTO WorkCenterStats(totalCapacity)
22 VALUES(new.capacity);
23 END IF;
24
25END $$
26
27DELIMITER ;
28
1CREATE TRIGGER trigger_name
2BEFORE INSERT
3 ON table_name FOR EACH ROW
4
5BEGIN
6
7 -- variable declarations
8
9 -- trigger code
10
11END;
1
2
3
4
5 INSERT INTO WorkCenters(name, capacity)
6VALUES('Mold Machine',100);
7