The infamous 20 year old MySQL Bug #11472 has been fixed

theanonymousone1 pts0 comments

MySQL Bugs: #11472: Triggers not executed following foreign key updates/deletes

Login /<br>Register

Developer Zone

Bugs Home

Report a bug

Advanced search

Saved searches

Tags

Bug #11472<br>Triggers not executed following foreign key updates/deletes

Submitted: 21 Jun 2005 0:08<br>Modified: 17 Mar 10:14

Reporter:

Omer Barnir<br>(OCA)<br>Email Updates:

Status:

Closed<br>Impact on me:

None

Category:MySQL Server: DML<br>Severity:S2 (Serious)

Version:5.0.8/5.5/5.6/5.7<br>OS:Any

Assigned to:

CPU Architecture:Any

View

Add Comment

Files

Developer

Edit Submission

View Progress Log

Contributions

[21 Jun 2005 0:08]<br>Omer Barnir

Description:<br>When rows of a table are updated/deleted indirectly as a result of a foregin key definition on the table, triggers on that table are not executed as required, as follows:

omer@linux:~/source/src50_0620/client> ./mysql --socket=/home/omer/source/src50_0620/mysql-test/var/tmp/master.sock --user=root<br>Welcome to the MySQL monitor. Commands end with ; or \g.<br>Your MySQL connection id is 1 to server version: 5.0.8-beta-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> USE test;<br>Database changed<br>mysql><br>mysql> DROP TABLE IF EXISTS t1,t2;<br>Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql><br>mysql> CREATE TABLE t1 (id INT NOT NULL, col1 char(50), PRIMARY KEY (id)) ENGINE=INNODB;<br>Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE t2 (id INT PRIMARY KEY, f_id INT, INDEX par_ind (f_id), col1 char(50),<br>-> FOREIGN KEY (f_id) REFERENCES t1(id) ON DELETE SET NULL) ENGINE=INNODB;<br>Query OK, 0 rows affected (0.01 sec)

mysql><br>mysql> create trigger tr_t2 after update on t2<br>-> for each row set @counter=@counter+1;<br>Query OK, 0 rows affected (0.00 sec)

mysql><br>mysql> insert into t1 values (1,'Department A');<br>Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 values (2,'Department B');<br>Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 values (3,'Department C');<br>Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values (1,2,'Emp 1');<br>Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values (2,2,'Emp 2');<br>Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values (3,2,'Emp 3');<br>Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values (4,2,'Emp 4');<br>Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values (5,2,'Emp 5');<br>Query OK, 1 row affected (0.00 sec)

mysql> set @counter=0;<br>Query OK, 0 rows affected (0.00 sec)

mysql> select * from t1;<br>+----+--------------+<br>| id | col1 |<br>+----+--------------+<br>| 1 | Department A |<br>| 2 | Department B |<br>| 3 | Department C |<br>+----+--------------+<br>3 rows in set (0.00 sec)

mysql> select * from t2;<br>+----+------+-------+<br>| id | f_id | col1 |<br>+----+------+-------+<br>| 1 | 2 | Emp 1 |<br>| 2 | 2 | Emp 2 |<br>| 3 | 2 | Emp 3 |<br>| 4 | 2 | Emp 4 |<br>| 5 | 2 | Emp 5 |<br>+----+------+-------+<br>5 rows in set (0.00 sec)

mysql> select @counter;<br>+----------+<br>| @counter |<br>+----------+<br>| 0 |<br>+----------+<br>1 row in set (0.00 sec)

mysql> delete from t1 where id=2;<br>Query OK, 1 row affected (0.05 sec)

mysql><br>mysql> select * from t1;<br>+----+--------------+<br>| id | col1 |<br>+----+--------------+<br>| 1 | Department A |<br>| 3 | Department C |<br>+----+--------------+<br>2 rows in set (0.00 sec)

mysql> select * from t2;<br>+----+------+-------+<br>| id | f_id | col1 |<br>+----+------+-------+<br>| 1 | NULL | Emp 1 |<br>| 2 | NULL | Emp 2 |<br>| 3 | NULL | Emp 3 |<br>| 4 | NULL | Emp 4 |<br>| 5 | NULL | Emp 5 |<br>+----+------+-------+<br>5 rows in set (0.00 sec)

mysql> select @counter;<br>+----------+<br>| @counter |<br>+----------+<br>| 0 |<br>+----------+<br>1 row in set (0.00 sec)

***** Note At this point 5 rows were updated in table t2, the value of<br>@count is expected to be '5' (each activation of the trigger<br>increases it by 1, and yet the value remained zero, indicating the<br>trigger was not executed.<br>The following shows that the trigger it self is executed when<br>table 't2' is updated directly:

mysql> update t2 set col1='Emp 5a' where id=5;<br>Query OK, 1 row affected (0.00 sec)<br>Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from t2;<br>+----+------+--------+<br>| id | f_id | col1 |<br>+----+------+--------+<br>| 1 | NULL | Emp 1 |<br>| 2 | NULL | Emp 2 |<br>| 3 | NULL | Emp 3 |<br>| 4 | NULL | Emp 4 |<br>| 5 | NULL | Emp 5a |<br>+----+------+--------+<br>5 rows in set (0.00 sec)

mysql> select @counter;<br>+----------+<br>| @counter |<br>+----------+<br>| 1 |<br>+----------+<br>1 row in set (0.00 sec)

***** In this case the trigger wasexecuted (@count set to '1')

mysql><br>mysql> drop table t2,t1;<br>Query OK, 0 rows affected (0.00 sec)

mysql> quit<br>Bye<br>omer@linux:~/source/src50_0620/client>

How to repeat:<br>Run the following in the mysql client:

USE test;

DROP TABLE IF EXISTS t1,t2;

CREATE TABLE t1 (id INT NOT NULL, col1 char(50), PRIMARY KEY (id)) ENGINE=INNODB;<br>CREATE TABLE t2 (id INT PRIMARY KEY, f_id INT, INDEX par_ind (f_id), col1 char(50),<br>FOREIGN KEY (f_id) REFERENCES t1(id) ON DELETE SET NULL) ENGINE=INNODB;

create trigger tr_t2 after update on t2<br>for each row set @counter=@counter+1;

insert into t1...

mysql query affected rows null table

Related Articles