11

For some reason, on a MySQL 5.5.30 machine, a trigger which deletes a row from a second table does no longer fire the delete trigger on the second table.

This works perfectly on our local MySQL version 5.5.25

I did not find any documentation that would explain this behaviour, does somebody maybe have an equal problem?

This is either a bug which occurs in MySQL version greater than 5.5.25 or a "feature" which is enabled accidently.

UPDATE table1 => fires BEFORE UPDATE trigger ON table1
      table1 BEFORE UPDATE TRIGGER executes: DELETE FROM table2 => should fire BEFORE DELETE trigger on table2 ( but doesn't )
            table 2 BEFORE DELETE TRIGGER executes: DELETE FROM table3 (never happens)

OK here my reproduce steps:

Database

CREATE DATABASE "triggerTest" DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Tables

CREATE TABLE "table1" (
  "id" int(11) NOT NULL AUTO_INCREMENT,
  "active" tinyint(1) NOT NULL DEFAULT '0',
  "sampleData" varchar(100) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  PRIMARY KEY ("id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC;


CREATE TABLE "table2" (
  "id" int(11) NOT NULL AUTO_INCREMENT,
  "table1_id" int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY ("id"),
  CONSTRAINT "test2_fk_table1_id" FOREIGN KEY ("table1_id") REFERENCES "table1" ("id") ON DELETE CASCADE ON UPDATE CASCADE  
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC;


CREATE TABLE "table3" (
  "id" int(11) NOT NULL AUTO_INCREMENT,
  "table1_id" int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY ("id"),
  CONSTRAINT "test3_fk_table1_id" FOREIGN KEY ("table1_id") REFERENCES "table1" ("id") ON DELETE CASCADE ON UPDATE CASCADE  
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC;

Triggers

DELIMITER $$

CREATE TRIGGER "table1_rtrg_AI" AFTER INSERT ON "table1" FOR EACH ROW
BEGIN
    IF NEW."active" THEN
        INSERT INTO "table2" ( "table1_id" ) SELECT NEW."id";
    END IF;
END$$

CREATE TRIGGER "table1_rtrg_BU" BEFORE UPDATE ON "table1" FOR EACH ROW
BEGIN
    IF NOT NEW."active" AND OLD."active" THEN
        DELETE FROM "table2" WHERE "table1_id" = OLD."id";
    END IF;

    IF NEW."active" AND NOT OLD."active" THEN
        INSERT INTO "table2" ( "table1_id" ) SELECT NEW."id";
    END IF;
END$$

CREATE TRIGGER "table2_rtrg_AI" AFTER INSERT ON "table2" FOR EACH ROW
BEGIN
    INSERT INTO "table3" ( "table1_id" ) SELECT NEW."table1_id";
END$$

CREATE TRIGGER "table2_rtrg_BD" BEFORE DELETE ON "table2" FOR EACH ROW
BEGIN
    DELETE FROM "table3" WHERE "table1_id" = OLD."table1_id";
END$$

DELIMITER ;

Q: Why do you quote identifiers using double quotes? (instead of backticks)

Because I don't like "niche syntax"

    mysql> show variables LIKE 'sql_mode';
+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value                                                                                                                                                |
+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_mode      | PIPES_AS_CONCAT,**ANSI_QUOTES**,IGNORE_SPACE,NO_UNSIGNED_SUBTRACTION,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Testcase 1: Expected behaviour (database version 5.2.20)

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.5.20    |
+-----------+
1 row in set (0.00 sec)

mysql> SET GLOBAL general_log := ON;

testing insert trigger

mysql> INSERT INTO "table1" ( "active", "sampleData" ) SELECT 0, 'sample data row 1';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

general_log: 
130423 12:51:27 78010 Query     INSERT INTO "table1" ( "active", "sampleData" ) SELECT 0, 'sample data row 1'


mysql> INSERT INTO "table1" ( "active", "sampleData" ) SELECT 1, 'sample data row 2';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

general_log:
130423 12:51:33 78010 Query     INSERT INTO "table1" ( "active", "sampleData" ) SELECT 1, 'sample data row 2'
                78010 Query     INSERT INTO "table2" ( "table1_id" ) SELECT NEW."id"
                78010 Query     INSERT INTO "table3" ( "table1_id" ) SELECT NEW."table1_id"

expected table contents:

mysql> SELECT * FROM "table1";
+----+--------+-------------------+
| id | active | sampleData        |
+----+--------+-------------------+
|  1 |      0 | sample data row 1 |
|  2 |      1 | sample data row 2 |
+----+--------+-------------------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM "table2";
+----+-----------+
| id | table1_id |
+----+-----------+
|  1 |         2 |
+----+-----------+
1 row in set (0.00 sec)

mysql> SELECT * FROM "table3";
+----+-----------+
| id | table1_id |
+----+-----------+
|  1 |         2 |
+----+-----------+
1 row in set (0.00 sec)

testing update trigger, set active

mysql> UPDATE "table1" SET "active" = 1 WHERE "id" = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

query_log:
130423 12:52:15 78010 Query     UPDATE "table1" SET "active" = 1 WHERE "id" = 1
                78010 Query     INSERT INTO "table2" ( "table1_id" ) SELECT NEW."id"
                78010 Query     INSERT INTO "table3" ( "table1_id" ) SELECT NEW."table1_id"

expected table contents:

mysql> SELECT * FROM "table1";
+----+--------+-------------------+
| id | active | sampleData        |
+----+--------+-------------------+
|  1 |      1 | sample data row 1 |
|  2 |      1 | sample data row 2 |
+----+--------+-------------------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM "table2";
+----+-----------+
| id | table1_id |
+----+-----------+
|  2 |         1 |
|  1 |         2 |
+----+-----------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM "table3";
+----+-----------+
| id | table1_id |
+----+-----------+
|  2 |         1 |
|  1 |         2 |
+----+-----------+
2 rows in set (0.00 sec)

testing update trigger, set inactive

mysql> UPDATE "table1" SET "active" = 0 WHERE "id" = 2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

query_log:

130423 12:52:49 78010 Query     UPDATE "table1" SET "active" = 0 WHERE "id" = 2
                78010 Query     DELETE FROM "table2" WHERE "table1_id" = NEW."id"
                78010 Query     DELETE FROM "table3" WHERE "table1_id" = OLD."table1_id"

expected table contents:

mysql> SELECT * FROM "table1";
+----+--------+-------------------+
| id | active | sampleData        |
+----+--------+-------------------+
|  1 |      1 | sample data row 1 |
|  2 |      0 | sample data row 2 |
+----+--------+-------------------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM "table2";
+----+-----------+
| id | table1_id |
+----+-----------+
|  2 |         1 |
+----+-----------+
1 row in set (0.00 sec)

mysql> SELECT * FROM "table3";
+----+-----------+
| id | table1_id |
+----+-----------+
|  2 |         1 |
+----+-----------+
1 row in set (0.00 sec)

Testcase2: unexpected behaviour (MySQL Version 5.5.30)

Holy triggers grml - You know what? Shame that I didn't test the second case first - unfortunately I was not able to reproduce the error.. the test worked on 5.5.30 as well, will keep you updated :)

EDIT Trigger did not cascade because of an unknown definer which was remaining in the sql dump made for production. Removing DEFINER= in the trigger dumps (alternative solution would be to create the user or to change DEFINER= to an existing one) solved the problem, solved a part of he problem.

The unknown definer did not cause any log file output

4

3 回答 3

8

最终结论:MySQL 5.5.30 在这种情况下没有问题,服务器本身也没有配置错误。

几个自己犯的错误导致了这个问题:

错误一:DEFINER 用户不存在

我不是只在生产机器上生成数据库,而是偷懒将测试数据库转储到生产机器上。如果您没有DEFINERCREATE TRIGGER语句中显式设置 a ,则将其设置为CURRENT_USER. CURRENT_USER不幸的是,在我的测试机器上,生产服务器上不存在这个确切的信息。

错误二:懒惰

mysqldump使用 DEFINER 转储触发器定义,创建触发器应该会生成一个警告,但是我又懒惰了,做了这样的事情..

mysqldump --triggers --routines -h test -p database | gzip -3 | ssh production "gunzip -c | mysql -h production_database_host -p production_database"

看起来很酷(omg geek)并为您节省了大量的转储文件推送,但它抑制了您在从控制台中加载转储时可能看到的警告

MySQL 写了以下关于触发器定义器的内容:

如果您指定 DEFINER 子句,这些规则将确定合法的 DEFINER 用户值:

如果您没有 SUPER 权限,则唯一合法的用户值是您自己的帐户,可以按字面意思指定,也可以使用 CURRENT_USER 指定。您不能将定义者设置为其他帐户。

如果您有 SUPER 权限,您可以指定任何语法上合法的帐户名称。如果该帐户实际上不存在,则会生成警告。

尽管可以使用不存在的 DEFINER 帐户创建触发器,但在帐户实际存在之前激活此类触发器并不是一个好主意。否则,关于权限检查的行为是未定义的。

来源:http ://dev.mysql.com/doc/refman/5.5/en/create-trigger.html

错误三:懒惰

我有一个非常酷的 mysqldump 包装器,它能够生成干净、可重用的转储文件。在没有 DEFINER 的情况下覆盖触发器时,我在生产服务器上打开了一个控制台事务(锁定 table2),因此 table2 上的触发器根本没有更新,而是再次更新,因为我的数据 sql 管道超过 5 个服务器,我没有看到超时错误。

结论:

没有错误,只是没有正确创建触发器..

有时候你应该停止偷懒,给重要的事情多一点时间和注意力可以为你节省很多时间!!

于 2013-04-24T15:24:52.970 回答
1

MySQL 中的触发器(与存储过程不同)总是在DEFINER. 触发器可能无法正常工作,因为它们DEFINER没有执行部分或全部触发器的权限。特别是,在 MySQL 5.1 及更高版本中,DEFINER需要具有TRIGGER特权以及相关SELECT和/或UPDATE特权。

当触发器似乎不起作用时,请检查权限。

于 2013-04-27T22:15:33.270 回答
0

请注意,外键操作不会触发触发器,请参阅触发器的限制

触发器不会被外键操作激活。

这意味着级联删除外键不会激活任何“ON DELETE”触发器......

于 2020-04-06T05:46:15.837 回答