mysql触发器的使用案例
先来说下我的案例:
青协活动通过excel上传活动信息,基本内容如下:
而我需要通过这张表的上传后自动跟新另外三张表的信息,一张是学生的总志愿时表跟新(某个学生的总志愿时再原来的基础上增多或者插入新的学生志愿时信息),一张是活动表的跟新,另一张是学院总志愿时的统计信息跟新(相同院的总志愿时)
此时,我所采用的是触发器跟新,当批量插入数据时,顺带触发三张表的跟新插入操作,具体的语法如下:
create trigger volunteer_1 after insert on volunteer_information for each row begin
if exists(select * from stu_volunteer A where A.stu_id=new.stu_id and A.year=SUBSTR(new.volunteer_time,1,4)) then update stu_volunteer set volunteer_time=volunteer_time+new.work_time where stu_id=new.stu_id and year=SUBSTR(new.volunteer_time,1,4);
else
insert into stu_volunteer(name,stu_id,profession,academy,volunteer_time,year)
VALUES(new.stu_name,new.stu_id,new.profession,new.academy,new.work_time,SUBSTR(new.volunteer_time,1,4));
END if;
if exists(select * from academy A where A.name=new.academy and A.year=SUBSTR(new.volunteer_time,1,4)) then update academy set volunteer_time=volunteer_time+new.work_time where name=new.academy and year=SUBSTR(new.volunteer_time,1,4);
else
insert into academy(name,volunteer_time,year)
VALUES(new.academy,new.work_time,SUBSTR(new.volunteer_time,1,4));
END if;
insert into volunteer_activity(volunteer_name,volunteer_time) select new.volunteer_name,SUBSTR(new.volunteer_time,1,4) from dual where not EXISTS(SELECT * from volunteer_activity where volunteer_name=new.volunteer_name and volunteer_time=SUBSTR(new.volunteer_time,1,4));
END
具体参考如下博客:
Mysql触发器 :当一个表发生插入或更新时,将数据同步到另一张表中
mysql判断表记录是否存在,不存在则插入新纪录
所遇到的错误
1、设置多个触发器;刚开始的想法是设置多个触发器来触发,可再设置完一个触发器,进行第二个触发器设置运行时,出现如下错误:
大体意思是说:一张表的某一个操作只能设置一个触发器,也就是针对志愿活动明细这张表的插入操作只能设置一个触发器,设置多个将报错。改进:将写的多个触发器归并成一个触发器去完成
create trigger volunteer_1 after insert on volunteer_information for each row begin
if exists(select * from stu_volunteer A where A.stu_id=new.stu_id and A.year=SUBSTR(new.volunteer_time,1,4)) then update stu_volunteer set volunteer_time=volunteer_time+new.work_time where stu_id=new.stu_id and year=SUBSTR(new.volunteer_time,1,4);
else
insert into stu_volunteer(name,stu_id,profession,academy,volunteer_time,year)
VALUES(new.stu_name,new.stu_id,new.profession,new.academy,new.work_time,SUBSTR(new.volunteer_time,1,4));
END if;
if exists(select * from academy A where A.name=new.academy and A.year=SUBSTR(new.volunteer_time,1,4)) then update academy set volunteer_time=volunteer_time+new.work_time where name=new.academy and year=SUBSTR(new.volunteer_time,1,4);
else
insert into academy(name,volunteer_time,year)
VALUES(new.academy,new.work_time,SUBSTR(new.volunteer_time,1,4));
END if;
insert into volunteer_activity(volunteer_name,volunteer_time) select new.volunteer_name,SUBSTR(new.volunteer_time,1,4) from dual where not EXISTS(SELECT * from volunteer_activity where volunteer_name=new.volunteer_name and volunteer_time=SUBSTR(new.volunteer_time,1,4));
END
2、设置触发器时,当表中插入的数据不在另一张表中时,进行插入操作。刚开始以为通过if not exists便可完成操作,但是执行语句时却报错。
解决:mysql的不存在该语法,使用错误,应为:
参考:mysql判断表记录是否存在,不存在则插入新纪录
3、注意:每一个if操作后都要进行end if说明再进行下一个if操作。old对应表中插入之前的数据,new表示插入的数据;mysql中的substr函数是对字符串进行切割,与正常字符串切割不同的是,它的开始是从1开始,结束是包括最后索引的值的。