拥有一个包含此架构的表
表演者(PerformerID,PerformerName,Street,City,State,Zip,ActivityID)
如果删除的ActivityID对表是唯一的,我需要定义一个触发器以防止删除。换句话说,如果某人尝试删除Performer,而他/她是唯一一个对整个表具有特定ActivityID的人,则触发器应该触发并避免删除。否则触发不应该中断。
我尝试了以下代码,但它给了我一个语法错误。
CREATE TRIGGER deletePerformer BEFORE DELETE ON Performers
FOR EACH ROW
BEGIN
If (Performers.ActivityID FROM INSERTED != Performers.ActivityID FROM Peformers)
Begin
RAISERROR ('Deletion is Not Allowed!', 16, 1)
Return
End
END;
任何帮助都得到了很多赞赏。
答案 0 :(得分:1)
正确的语法看起来像这样:
DELIMITER $$
CREATE TRIGGER deletePerformer BEFORE DELETE ON Performers
FOR EACH ROW
BEGIN
If (NOT EXISTS (SELECT 1
FROM Performers p2
WHERE p2.ActivityID = old.ActivityId AND
p2.PerformerID <> old.PerformerId
)
) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Deletion is Not Allowed!';
End if;
END
$$
DELIMITER ;
但是,我不确定这是执行您要强制执行的规则的最佳方法。