使用Mysql Trigger删除行

时间:2011-08-26 18:43:39

标签: mysql triggers

需要在名为jos_jquarks_quizzes的表上插入后触发,我需要创建一个与quizz名称相同的课程名称,但是它自己的id,

jos_jquarks_quizzes 

id     title     description     course_id 

jos_jquarks_users_training   
id     quiz_id     user_id     agree 

到目前为止,我的触发器看起来像这样但有错误

-- Trigger DDL Statements
DELIMITER $$

USE `db_dhruniversity`$$

CREATE
TRIGGER `db_dhruniversity`.`ai_delete_course`
AFTER DELETE ON `jos_jquarks_quizzes`
FOR EACH ROW
BEGIN    
DELETE FROM jos_jquarks_courses
WHERE (quiz_id = new.id); 
END$$

1 个答案:

答案 0 :(得分:8)

在你的where子句中将new.id更改为old.id

-- Trigger DDL Statements
DELIMITER $$

USE `db_dhruniversity`$$

CREATE
TRIGGER `db_dhruniversity`.`ai_delete_course`
AFTER DELETE ON `jos_jquarks_quizzes`
FOR EACH ROW
BEGIN    
DELETE FROM jos_jquarks_courses
WHERE (quiz_id = old.id); 
END$$