mysql触发器一个表改变另一个表也改变问题
更新时间:2023年08月29日 11:40:37 作者:Raya_L
这篇文章主要介绍了mysql触发器一个表改变另一个表也改变问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
mysql触发器一个表改变另一个也改变
主表:
副表:
删除:
DELIMITER $$ USE `motooling`$$ DELIMITER ; DROP TRIGGER IF EXISTS `pm_pg_member_delete`; DELIMITER ;; CREATE TRIGGER `pm_pg_member_delete` AFTER DELETE ON `user_company` FOR EACH ROW BEGIN SELECT dep_id INTO @procId FROM department WHERE dep_code='Production_Dept' AND company_id=old.company_id; SELECT dep_id INTO @depIds FROM department WHERE dep_id=old.dep_id AND CONCAT('|', all_upper_ids, '|') REGEXP CONCAT('|', @procId, '|'); IF old.dep_id IN (@depIds) THEN DELETE FROM pm_pg_member WHERE pg_id=old.dep_id AND member_id=old.uid; END IF; END ;; DELIMITER ;
添加:
DELIMITER $$ USE `motooling`$$ DELIMITER ; DROP TRIGGER IF EXISTS `pm_pg_member_insert`; DELIMITER ;; CREATE TRIGGER `pm_pg_member_insert` AFTER INSERT ON `user_company` FOR EACH ROW BEGIN SELECT dep_id INTO @procId FROM department WHERE dep_code='Production_Dept' AND company_id=new.company_id; SELECT dep_id INTO @depIds FROM department WHERE dep_id=new.dep_id AND CONCAT('|', all_upper_ids, '|') REGEXP CONCAT('|', @procId, '|'); IF new.dep_id IN (@depIds) THEN SELECT dep_id INTO @depId FROM user_company WHERE id=new.id; SELECT uid INTO @uid FROM user_company WHERE id=new.id; SELECT work_name INTO @workName FROM user_company WHERE id=new.id; SELECT is_leader INTO @isLeader FROM user_company WHERE id=new.id; SELECT pri INTO @pri FROM user_company WHERE id=new.id; SELECT is_schedule INTO @isSchedule FROM user_company WHERE id=new.id; SELECT skill_level_id INTO @skillLevelId FROM user_company WHERE id=new.id; SELECT skill_level_name INTO @skillLevelName FROM user_company WHERE id=new.id; SELECT furlough_start_date INTO @furloughStartDate FROM user_company WHERE id=new.id; SELECT furlough_end_date INTO @furloughEndDate FROM user_company WHERE id=new.id; SELECT is_show_effic INTO @isShowEffic FROM user_company WHERE id=new.id; SELECT work_state INTO @workState FROM user_company WHERE id=new.id; SELECT last_pop_id INTO @LastPopId FROM user_company WHERE id=new.id; SELECT handler_id INTO @handlerId FROM user_company WHERE id=new.id; SELECT created_at INTO @createdAt FROM user_company WHERE id=new.id; SELECT updated_at INTO @updatedAt FROM user_company WHERE id=new.id; SELECT STATUS INTO @status FROM user_company WHERE id=new.id; INSERT INTO pm_pg_member (pg_id, member_id, member_name, is_leader, pri, is_schedule, skill_level_id, skill_level_name, furlough_start_date, furlough_end_date, is_show_effic, member_status, last_pop_id, handler_id, created_at, updated_at, STATUS) VALUES (@depId,@uid,@workName,@isLeader,@pri,@isSchedule,@skillLevelId,@skillLevelName,@furloughStartDate,@furloughEndDate,@isShowEffic,@workState ,@LastPopId,@handlerId,@createdAt, @updatedAt,@status); END IF; END ;; DELIMITER ;
修改:
DELIMITER $$ USE `motooling`$$ DELIMITER ; DROP TRIGGER IF EXISTS `pm_pg_member_update`; DELIMITER ;; CREATE TRIGGER `pm_pg_member_update` AFTER UPDATE ON `user_company` FOR EACH ROW BEGIN SELECT dep_id INTO @procId FROM department WHERE dep_code='Production_Dept' AND company_id=new.company_id; SELECT dep_id INTO @depIds FROM department WHERE dep_id=new.dep_id AND CONCAT('|', all_upper_ids, '|') REGEXP CONCAT('|', @procId, '|'); IF new.dep_id IN (@depIds) THEN UPDATE pm_pg_member SET pg_id=new.dep_id, member_id=new.uid, member_name=new.work_name, is_leader=new.is_leader, pri=new.is_leader, is_schedule=new.is_schedule, skill_level_id=new.skill_level_id, skill_level_name=new.skill_level_name, furlough_start_date=new.furlough_start_date, furlough_end_date=new.furlough_end_date, is_show_effic=new.is_show_effic, member_status=new.work_state, last_pop_id=new.last_pop_id, handler_id=new.handler_id, created_at=new.created_at, updated_at=new.updated_at, STATUS=new.status WHERE pg_id=old.dep_id AND member_id=old.uid; END IF; END ;; DELIMITER ;
注:删除只有old,新增只有new,修改既有old又有new
mysql触发器:A表更新B表新增
创建触发器
CREATE TRIGGER T_TRIGGER_UPDATE BEFORE|AFTER UPDATE|INSERT|DELETE ON 表A FOR EACH ROW BEGIN insert into 表B(列1, 列2, ...) VALUES(NEW.列1, NEW.列2, ...); END
解释:
T_TRIGGER_UPDATE为触发器名字;BEFORE|AFTER UPDATE|INSERT|DELETE:在更新之前或之后进行更新,插入或删除操作;
NEW表示进行相应操作的数据,如进行更新A表的操作,就表示要修改的A表的数据。
查看输出器
SHOW TRIGGERS;
删除指定的触发器
DROP trigger T_TRIGGER_UPDATE
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SQLyog的下载、安装、破解、配置教程(MySQL可视化工具安装)
SQLyog是一款MySQL可视化工具,他可以将部分SQL操作通过图形化界面操作来完成,方便开发者更好的进行开发及数据库设计,这篇文章主要介绍了SQLyog的下载、安装、破解、配置(MySQL可视化工具安装),需要的朋友可以参考下2022-09-09mysql密码忘记后如何修改密码(2022年最新版详细教程保姆级)
因为长时间不操作mysql而忘记root密码的朋友估计不在少数,下面这篇文章主要给大家介绍了关于mysql密码忘记后如何修改密码的相关资料,本教程是2022年最新版详细教程保姆级,需要的朋友可以参考下2022-04-04centos7环境下二进制安装包安装 mysql5.6的方法详解
这篇文章主要介绍了centos7环境下二进制安装包安装 mysql5.6的方法,详细分析了centos7环境下使用二进制安装包安装 mysql5.6的具体步骤、相关命令、配置方法及操作注意事项,需要的朋友可以参考下2020-02-02
最新评论