如何在 MySQL 中禁用触发器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13598155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:35:40  来源:igfitidea点击:

How to disable triggers in MySQL?

mysqldatabase-trigger

提问by Grijesh Chauhan

In my MySQL database I have some triggers ON DELETEand ON INSERT. Sometimes I need to disable some triggers, and I have to DROPe.g.

在我的 MySQL 数据库中,我有一些触发器ON DELETEON INSERT. 有时我需要禁用一些触发器,我必须DROP例如

DROP TRIGGER IF EXISTS hostgroup_before_insert //   

and reinstall. Is there any shortcut to SET triggers hostgroup_before_insert = 0like we have for foreign keys:

并重新安装。是否有任何快捷方式SET triggers hostgroup_before_insert = 0类似于我们对外键的使用:

mysql> SELECT version();
+-------------------------+
| version()               |
+-------------------------+
| 5.1.61-0ubuntu0.10.10.1 |
+-------------------------+
1 row in set (0.00 sec)

EDITAnswer There is no built-in server system variable TRIGGER_CHECKS in MySQL.
A simple workaround is to instead use a user-defined session variable.

编辑答案MySQL 中没有内置服务器系统变量 TRIGGER_CHECKS
一个简单的解决方法是使用用户定义的会话变量。

#FALSE value overrides trigger type settings
SET @TRIGGER_CHECKS = [TRUE|FALSE]; 

SET @TRIGGER_BEFORE_INSERT_CHECKS = [TRUE|FALSE];
SET @TRIGGER_AFTER_INSERT_CHECKS = [TRUE|FALSE];

DELIMITER $$
DROP TRIGGER IF EXISTS `yearCheck_beforeInsert` $$
CREATE DEFINER=`root`@`localhost` TRIGGER `yearCheck_beforeInsert`
BEFORE INSERT ON `movies` FOR EACH ROW 

#Patch starts here
thisTrigger: BEGIN
  IF ((@TRIGGER_CHECKS = FALSE)
      OR (@TRIGGER_BEFORE_INSERT_CHECKS = FALSE))
    AND (USER() = 'root@localhost') 

This TRICK is Explained here.

这个技巧在这里解释

回答by Fathah Rehman P

Its not possible to temporarly disable triggers. Do one thing, use one global variable. Trigger will first check value of global variable first. In this case you can change value of global variable to prevent working of trigger.

暂时禁用触发器是不可能的。做一件事,使用一个全局变量。触发器将首先检查全局变量的值。在这种情况下,您可以更改全局变量的值以防止触发器工作。