MySQL disable all triggers

旧城冷巷雨未停 提交于 2019-11-26 16:53:05

问题


For test correctness of query I need disable all triggers in db. I see that in information_schema exists table TRIGGERS. Is possible temporarily disable all triggers using this table? E.g. like:

update TRIGGERS set TRIGGERS_SCHEMA='myschema_new' 
where TRIGGERS_SCHEMA='myschema'

and after finish all test return all triggers like:

update TRIGGERS set TRIGGERS_SCHEMA='myschema'
where TRIGGERS_SCHEMA='myschema_new'

May be this can corrupt db or after triggers will not works? I didn't found about it in documentation.


回答1:


You can't disable triggers directly and I wouldn't recommend doing what you're suggesting but you could have your trigger check if a variable (in my example below @disable_triggers) is NULL before executing the trigger's content. For example:

Query:

SET @disable_triggers = 1;
// Your update statement goes here.
SET @disable_triggers = NULL;

Triggers:

IF @disable_triggers IS NULL THEN
    // Do something use as the trigger isn't disabled.
END IF;



回答2:


It is not possible to 'disable' triggers in mysql, however a trick that can be used to get around this

Add a condition in your triggers like:

if (DISABLE_TRIGER <> 1 ) then 
#trigger body 
end if; 

and than if you want to disable triggers on import just:

SET @DISABLE_TRIGER = 1;

do imports

SET @DISABLE_TRIGER = 0;


来源:https://stackoverflow.com/questions/6480074/mysql-disable-all-triggers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!