56,687
社区成员
发帖
与我相关
我的任务
分享
mysql> create table a (id int,s int);
Query OK, 0 rows affected (0.09 sec)
mysql> create table b (s int primary key);
Query OK, 0 rows affected (0.06 sec)
mysql>
mysql> delimiter |
mysql>
mysql> CREATE TRIGGER tr_a_bi BEFORE INSERT ON a
-> FOR EACH ROW BEGIN
-> INSERT INTO b SET s = NEW.s;
-> END;
-> |
Query OK, 0 rows affected (0.08 sec)
mysql>
mysql> delimiter ;
mysql>
mysql> insert into a values (1,1);
Query OK, 1 row affected (0.05 sec)
mysql> select * from a;
+------+------+
| id | s |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)
mysql> select * from b;
+---+
| s |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
mysql>
mysql> insert into a values (2,1);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> select * from a; -- 记录未插入。如果不生成,所有回滚
+------+------+
| id | s |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)
mysql> select * from b;
+---+
| s |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
mysql>