2

我是触发器的新手,并且收到“一个表具有相同操作时间和事件的多个触发器”错误。

我创建了一个 AFTER Update 和一个 AFTER Delete ,它们是两个单独的操作时间/事件,所以我不确定为什么会收到错误。

这是我的查询:

CREATE TRIGGER `new_enrolment` AFTER INSERT ON `mdl_user_enrolments` FOR EACH ROW BEGIN
INSERT INTO c_master (
ud,
firstname,
lastname,
email,
username,
cid,
course
)
SELECT 
mdl_user.id AS uid, 
mdl_user.firstname, 
mdl_user.lastname, 
mdl_user.email, 
mdl_user.suspended, 
mdl_user.username, 
mdl_enrol.courseid AS cid,
mdl_course.fullname AS course
FROM mdl_user_enrolments INNER JOIN mdl_enrol ON mdl_user_enrolments.enrolid = mdl_enrol.id
 INNER JOIN mdl_course ON mdl_enrol.courseid = mdl_course.id
 INNER JOIN mdl_user ON mdl_user.id = mdl_user_enrolments.userid
WHERE userid = NEW.userid;
END;

CREATE TRIGGER `remove_enrolment` AFTER DELETE ON `mdl_user_enrolments` FOR EACH ROW BEGIN
SELECT mdl_enrol.courseid, 
mdl_user_enrolments.userid, 
mdl_user_enrolments.enrolid
FROM mdl_user_enrolments INNER JOIN mdl_enrol ON mdl_user_enrolments.enrolid = mdl_enrol.id
WHERE mdl_user_enrolments.enrolid = OLD.enrolid

DELETE FROM c_master
WHERE uid = OLD.userid AND mdl_enrol.courseid;
END;

由于我对这一切都不熟悉,因此我可能缺少一些简单的东西。

4

1 回答 1

4

Brian,可能您已经在表中添加和删除了几次相同的触发器,而您上次忘记删除它,然后再次创建它。确保在再次使用创建之前已将其删除

DROP TRIGGER [IF EXISTS] [schema_name.]trigger_name

进而

CREATE TRIGGER ....

文档在这里

于 2013-06-25T23:58:20.977 回答