触发MySQL不起作用

时间:2013-07-12 22:50:16

标签: mysql database triggers

USE `db`;
DELIMITER $$

CREATE TRIGGER `recintos_BDEL` 
BEFORE DELETE ON recintos 
FOR EACH ROW 
BEGIN
DELETE FROM imagenes WHERE id_imagen IN (
    SELECT imagenes_id_imagen FROM r_imagenes_recintos 
    WHERE recintos_id_recinto=OLD.id_recinto
    );
END; $$
DELIMITER |

OR

USE `db`;
DELIMITER $$

CREATE TRIGGER `recintos_BDEL` 
BEFORE DELETE ON recintos 
FOR EACH ROW
DELETE FROM imagenes WHERE id_imagen IN (
    SELECT imagenes_id_imagen FROM r_imagenes_recintos 
    WHERE recintos_id_recinto=NEW.id_recinto
);

1 个答案:

答案 0 :(得分:0)

唯一的区别似乎是在第一个实例中使用了BEGIN ... END复合语句块(并且在第二个实例中错误地更改了/缺少关闭分隔符)。

由于触发器只包含一个语句,因此BEGIN ... END复合语句块没有 need - 但它肯定不会造成任何伤害。也就是说,通过更正使用语句分隔符,两种方法都可以达到相同的结果。

对于它的价值,我个人的偏好是使用第二个实例(即避免复合语句块,它们不是绝对必要的。)