[英]Trigger After Delete Not working
Here is my code 这是我的代码
CREATE TRIGGER `agent_maintenance` AFTER DELETE ON `users`
FOR EACH ROW BEGIN
INSERT INTO deleted_agents (agent_id, fullname, email, mobile_no, deleted)
SELECT user_id, fullname, email, mobile_no, NOW() FROM user_profiles WHERE user_id = OLD.id;
END
//
DELIMITER ;
Apparently nothing got inserted to table deleted_agents 显然没有任何内容插入表Deleted_agents
But when i change the event to BEFORE DELETE
it work just fine. 但是,当我将事件更改为
BEFORE DELETE
它可以正常工作。
Anyone know what is wrong? 有人知道怎么了吗?
EDITED 已编辑
Yes there is foreign key constraint inside table user_profiles. 是的,表user_profiles中有外键约束。
Basically when a row from table user
got deleted, it will delete a coresponding row on table user_profiles
. 基本上,当删除表
user
的行时,它将删除表user_profiles
行。
So i make a wrong assumption that a trigger would be executed first before any foreign key constraint action 因此,我做出了错误的假设,即在任何外键约束操作之前先执行触发器
The best way to do this is to use AFTER DELETE
trigger on table user_profiles itself. 最好的方法是对表user_profiles本身使用
AFTER DELETE
触发器。
Unfortunately mysql dosent activate the trigger on foreign key constraint 不幸的是, MySQL的剂量在外键约束激活触发器
难道是因为已删除该行,所以对user_profiles表执行了级联删除触发器,并且WHERE子句找不到user_id?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.