如何在MYSQL中编写一个触发器来中止删除?

发布于 2024-12-07 04:38:22 字数 649 浏览 2 评论 0原文

我读了这个文章 但它似乎不适用于删除。尝试创建触发器时出现此错误:

在服务器中执行SQL脚本

错误:错误 1363:DELETE 触发器中没有新行

CREATE TRIGGER DeviceCatalog_PreventDeletion 
BEFORE DELETE on DeviceCatalog
FOR EACH ROW
BEGIN
    DECLARE dummy INT;

    IF old.id = 1 or old.id =2 THEN
        SELECT * FROM DeviceCatalog WHERE DeviceCatalog.id=NEW.id;
    END IF;
END; 

SQL脚本执行完成:语句:4条成功,1条失败

I read this article but it seems not work for delete. I got this error when tried to create a trigger:

Executing SQL script in server

ERROR: Error 1363: There is no NEW row in on DELETE trigger

CREATE TRIGGER DeviceCatalog_PreventDeletion 
BEFORE DELETE on DeviceCatalog
FOR EACH ROW
BEGIN
    DECLARE dummy INT;

    IF old.id = 1 or old.id =2 THEN
        SELECT * FROM DeviceCatalog WHERE DeviceCatalog.id=NEW.id;
    END IF;
END; 

SQL script execution finished: statements: 4 succeeded, 1 failed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自控 2024-12-14 04:38:22

尝试这样的事情 -

DELIMITER $

CREATE TRIGGER trigger1
BEFORE DELETE
ON table1
FOR EACH ROW
BEGIN
  IF OLD.id = 1 THEN -- Abort when trying to remove this record
    CALL cannot_delete_error; -- raise an error to prevent deleting from the table
  END IF;
END
$

DELIMITER ;

Try something like this -

DELIMITER $

CREATE TRIGGER trigger1
BEFORE DELETE
ON table1
FOR EACH ROW
BEGIN
  IF OLD.id = 1 THEN -- Abort when trying to remove this record
    CALL cannot_delete_error; -- raise an error to prevent deleting from the table
  END IF;
END
$

DELIMITER ;
懒猫 2024-12-14 04:38:22

使用@MathewFoscarini关于MySQL SIGNAL 命令,您可以发出自定义错误消息信号,而不是通过调用不存在的过程来引发错误。

DELIMITER $

CREATE TRIGGER DeviceCatalog_PreventDeletion
BEFORE DELETE ON DeviceCatalog
FOR EACH ROW
BEGIN

  IF old.id IN (1,2) THEN -- Will only abort deletion for specified IDs
    SIGNAL SQLSTATE '45000' -- "unhandled user-defined exception"
      -- Here comes your custom error message that will be returned by MySQL
      SET MESSAGE_TEXT = 'This record is sacred! You are not allowed to remove it!!';
  END IF;

END
$

DELIMITER ;

SQLSTATE 45000被选为MySQL参考手册< /a> 建议:

要表示通用 SQLSTATE 值,请使用“45000”,这意味着“未处理的用户定义的异常”。

这样,每当用户尝试删除 ID 12 记录时,您的自定义消息就会显示给用户。此外,如果不应从表中删除任何记录,您只需删除 IF .. THENEND IF; 行即可。这将防止删除表中的任何记录。

Improving @Devart's (accepted) answer with @MathewFoscarini's comment about MySQL SIGNAL Command, instead of raising an error by calling an inexistent procedure you could signal your custom error message.

DELIMITER $

CREATE TRIGGER DeviceCatalog_PreventDeletion
BEFORE DELETE ON DeviceCatalog
FOR EACH ROW
BEGIN

  IF old.id IN (1,2) THEN -- Will only abort deletion for specified IDs
    SIGNAL SQLSTATE '45000' -- "unhandled user-defined exception"
      -- Here comes your custom error message that will be returned by MySQL
      SET MESSAGE_TEXT = 'This record is sacred! You are not allowed to remove it!!';
  END IF;

END
$

DELIMITER ;

The SQLSTATE 45000 was chosen as MySQL's Reference Manual suggests:

To signal a generic SQLSTATE value, use '45000', which means “unhandled user-defined exception.”

This way your custom message will be shown to the user whenever it tries to delete records ID 1 or 2. Also, if no records should be deleted from the table, you could just remove the IF .. THEN and END IF; lines. This would prevent ANY records from being deleted on the table.

真心难拥有 2024-12-14 04:38:22

嗯,错误消息非常清楚地告诉您:在 DELETE 触发器中没有 NEW。

  • 在 INSERT 触发器中,您可以使用 NEW. 访问新值。
  • 在 UPDATE 触发器中,您可以使用 NEW. 访问新值,使用 - 您猜对了 - OLD 访问旧值。
  • 在 DELETE 触发器中,您可以使用 OLD 访问旧值。

在 DELETE 中使用 NEW 毫无意义,就像在 INSERT 中使用 OLD 毫无意义一样。

Well, the error messages tells you quite clearly: in a DELETE trigger there is no NEW.

  • In an INSERT trigger you can access the new values with NEW..
  • In an UPDATE trigger you can access the new values with NEW., the old ones with - you guessed it - OLD.
  • In a DELETE trigger you can acces the old values with OLD..

It simply makes no sense to have NEW in a DELETE, just as OLD in an INSERT makes no sense.

亽野灬性zι浪 2024-12-14 04:38:22

正如错误所示:删除时没有新变量。

您只能在插入和更新时使用 new.id 。请改用 old.id。

SELECT * FROM DeviceCatalog WHERE DeviceCatalog.id=old.id;

As the error says: There is no NEW variable on delete.

you can use new.id only on insert and update. Use old.id instead.

SELECT * FROM DeviceCatalog WHERE DeviceCatalog.id=old.id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文