mysql的触发器:
简单的例子:有班级表(classID,stuCount)和学生表(stuID,classID)
要求班级表的stuCount随着学生表的行数增加而增加(无需作外键关联)
1 DELIMITER $$ 2 CREATE 3 /*[DEFINER = { user | CURRENT_USER }]*/ 4 TRIGGER `mianshi`.`tr_add` AFTER INSERT 5 ON `mianshi`.`student` 6 FOR EACH ROW 7 BEGIN 8 DECLARE c INT; 9 SET c = (SELECT stuCount FROM class WHERE classID=new.classID); 10 UPDATE class SET stuCount = c + 1 WHERE classID=new.classID; 11 END$$ 12 13 DELIMITER ;
触发器无需手动调用