DELIMITER $$
CREATE TRIGGER `product_BEFORE_UPDATE` BEFORE UPDATE ON product
FOR EACH ROW
BEGIN
IF ((SELECT 1 FROM sale WHERE product_id=NEW.product_id)=true) THEN
set @message_text = concat('Product with id: ', NEW.product_id , ' exist on Sale table');
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = @message_text;
END IF;
END$$
DELIMITER ;
您可以使用以下触发器:
每次更新
product
表之前,触发器都会检查表product_id
上是否存在sale
并抛出如下错误:以下所有测试数据都可以在小提琴上找到