Starting MySQL before inserting with INSERT IGNORE

Is MySQL a trigger that is set before an insert into a specific table is executed when I call INSERT IGNORE and the insert is ignored?

+3


source to share


1 answer


Here's a demo:

mysql> create table foo (foo_id int primary key);

mysql> create table bar (foo_id int);

mysql> create trigger foo_ins before insert on foo 
       for each row insert into bar set foo_id = new.foo_id;

mysql> insert ignore into foo set foo_id=123;
Query OK, 1 row affected (0.01 sec)

mysql> insert ignore into foo set foo_id=123;
Query OK, 0 rows affected, 1 warning (0.00 sec)

      

This should only have inserted one row into the foo table because the second attempt conflicts with the primary key value. We can see that the second insert affects 0 rows, which means no new row has been inserted.



Let's see what affects the column table:

mysql> select * from bar;
+--------+
| foo_id |
+--------+
|    123 |
|    123 |
+--------+

      

Two lines have been inserted. This proves that the trigger fires even if the insert is ignored.

+2


source







All Articles