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