如果不存在,Mysql会创建触发器

时间:2015-08-24 06:53:51

标签: mysql triggers

我有一个创建触发器的脚本, 我想多次运行它,如果已经存在触发器,则需要跳过创建部分。

是否存在"如果不存在则创建"在mysql触发器?

编辑:

我试过了:

    IF NOT EXISTS ((SELECT TRIGGER_NAME
        FROM information_schema.triggers
        WHERE TRIGGER_SCHEMA = 'xxx_admin' AND TRIGGER_NAME = 'test_AFTER_UPDATE')) 
        THEN
    CREATE DEFINER=`root`@`localhost` TRIGGER `xxx_admin`.`test_AFTER_UPDATE` AFTER UPDATE ON `test` FOR EACH ROW
        BEGIN
        INSERT INTO auditTest 
        select *, now() from test where id = NEW.id;
        END;
  END IF;

我得到:syntex错误" ' IF'在此位置无效输入

2 个答案:

答案 0 :(得分:1)

要检查是否存在触发器,请使用以下

SELECT *
FROM information_schema.triggers
where trigger_schema = 'schema_name'
and trigger_name = 'triggername'

答案 1 :(得分:0)

您可以查看INFORMATION_SCHEMA现有触发器(此answer的信用额度):

SELECT trigger_schema, trigger_name, action_statement
    FROM information_schema.triggers

如果存在特定的trigger,则可以跳过创建它:

IF (NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_SCHEMA = 'your_schema' AND TRIGGER_NAME = 'your_trigger_name'))
    CREATE TRIGGER ...