无法创建/删除触发器

时间:2015-11-12 08:20:27

标签: mysql triggers mysql-workbench

我正在尝试重新创建我之前创建并删除的触发器。

删除SQL:

DROP TRIGGER openitdb.trgLicenseInsert;

删除错误:

  

错误代码:1360。触发器不存在

创建SQL:

DELIMITER $$

CREATE TRIGGER trgLicenseInsert
AFTER UPDATE ON SoftwareLicenseDetails
FOR EACH ROW BEGIN

INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', (SELECT Title FROM SoftwareTypes WHERE ID = (SELECT SoftwareNameFK
FROM SoftwareLicenseDetails
WHERE  ComputerFK=new.ComputerFK
ORDER BY ID Desc
LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);

END; $$
DELIMITER ;

创建错误:

  

错误代码:1359。触发器已存在

触发器没有显示在MySQL Workbench中,但不知何故它被标记为已经存在。

SoftwareLicenseDetails table information

SHOW TRIGGERS

enter image description here

我该如何解决这个问题?我错过了什么吗?

修改

我尝试合并两个触发器,但我在第6行收到错误(IF new.Flag = 0 ..)

DELIMITER $$

CREATE TRIGGER trgSoftwareLicenseDetails
AFTER UPDATE ON SoftwareLicenseDetails
FOR EACH ROW BEGIN
IF new.Flag = 0 THEN
    INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', (SELECT Title FROM SoftwareTypes WHERE ID = 
    (SELECT SoftwareNameFK FROM SoftwareLicenseDetails
    WHERE  ComputerFK=new.ComputerFK
    ORDER BY ID Desc
    LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);
FROM SoftwareLicenseDetails
WHERE  ComputerFK=new.ComputerFK
ORDER BY ID Desc
LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);
ELSE IF new.Flag = 1 THEN
    INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', 'N/A', (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Deleted',old.ComputerFK);
END IF;
END; $$
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

根据show triggers命令,trgLicenseDelete触发器附加到更新事件之后。但是,您尝试通过将trgLicenseInsert附加到同一事件(更新后)来创建trgLicenseInsert,因此mysql认为trgLicenseInsert触发器是重复的,即使它们的名称不同。显然,您不能使用不存在的名称删除触发器。

如果两个触发器都需要在更新后运行,那么我建议您将其功能合并到一个触发器中,或者需要将2个触发器附加到不同的事件。

但是,您可能正在使用旧版本的mysql,因为如果复制是由于同一事件而不是同一个名称,则较新版本会产生不同的错误消息,请参阅mysql bug 10946