MySQL禁用所有触发器

问题描述:

为了测试查询的正确性,我需要禁用db中的所有触发器. 我看到在information_schema中存在表TRIGGERS. 是否可以使用此表暂时禁用所有触发器? 例如.像:

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'

这可能会损坏db还是在触发后将不起作用?我没有在文档中找到它.

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

您不能直接禁用触发器,我也不建议您执行建议的操作,但是可以让触发器检查变量(在我的示例中)在执行触发器的内容之前,@disable_triggers以下的是NULL.例如:

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:

查询:

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

触发器:

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