DELETE触发器不工作 - MYSQL

时间:2016-02-19 12:49:56

标签: mysql events triggers

第一张表 - rm_desc
第二个表 - room_cat_mapping

想要实现:在自动删除rm_desc中的记录时,它也应该从room_cat_mapping中删除相关记录。

但是下面的触发器没有工作,任何新的触发器

DROP TRIGGER IF EXISTS DELETE_ROOM_TYPES;
DELIMITER $$

CREATE
    /*[DEFINER = { user | CURRENT_USER }]*/
    TRIGGER `DELETE_ROOM_TYPES` BEFORE DELETE
    ON `rm_desc`
    FOR EACH ROW 
    BEGIN
        DELETE FROM room_cat_mapping
        WHERE room_cat_mapping.prop_id = rm_desc.res AND room_cat_mapping.room_cat_id = rm_desc.rm_cat;
    END$$
DELIMITER ;

1 个答案:

答案 0 :(得分:1)

这应该做:

DROP TRIGGER IF EXISTS DELETE_ROOM_TYPES;
DELIMITER $$

CREATE

    TRIGGER `DELETE_ROOM_TYPES` AFTER DELETE
    ON `rm_desc`
    FOR EACH ROW 
    BEGIN
        DELETE FROM room_cat_mapping
        WHERE room_cat_mapping.prop_id = OLD.res AND room_cat_mapping.room_cat_id = OLD.rm_cat;
    END$$
DELIMITER ;

我将触发器更改为 AFTER ,并使用 OLD 引用已删除的行值。