插入INSERT IGNORE之前的MySQL触发器

时间:2017-06-22 07:17:22

标签: mysql triggers

在插入到某个表之前设置的MySQL触发器是在我调用INSERT IGNORE并且忽略插入时执行的吗?

1 个答案:

答案 0 :(得分:4)

这是一个示范:

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)

这应该只在foo表中插入一行,因为第二次尝试会与主键值冲突。我们看到第二个插入影响0行,这意味着没有插入新行。

让我们看看吧台上的效果是什么:

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

插入两行。这证明即使进行插入忽略也会触发触发器。

相关问题