竹子是花中君子,高雅挺立;竹子是心灵支柱,坚韧不拔;竹子是勉励之歌,节节攀升!
下面是一种实现的方法。思路就是想办法在触发器中利用一个出错的语句来中断代码的执行。 mysql> create table t_control(id int primary key);
Query OK, 0 rows affected (0.11 sec)
mysql> insert into t_control values (1);
Query OK, 1 row affected (0.05 sec)
mysql> create table t_bluerosehero(id int primary key,col int);
Query OK, 0 rows affected (0.11 sec)
mysql> delimiter //
mysql> create trigger tr_t_bluerosehero_bi before insert on t_bluerosehero
-> for each row
-> begin
-> if new.col>30 then
-> insert into t_control values (1);
-> end if;
-> end;
-> //
Query OK, 0 rows affected (0.08 sec)
mysql> delimiter ;
mysql>
mysql> insert into t_bluerosehero values (1,20);
Query OK, 1 row affected (0.25 sec)
mysql> insert into t_bluerosehero values (2,40);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql>
mysql> select * from t_bluerosehero;
+----+------+
| id | col |
+----+------+
| 1 | 20 |
+----+------+
1 row in set (0.00 sec)
mysql>
或者
mysql> delimiter //
mysql> create trigger tr_t_bluerosehero_bi before insert on t_bluerosehero
-> for each row
-> begin
-> declare i int;
-> if new.col>30 then
-> insert into xxxx values (1);
-> end if;
-> end;
-> //
Query OK, 0 rows affected (0.06 sec)
mysql> delimiter ;
mysql> delete from t_bluerosehero;
Query OK, 3 rows affected (0.05 sec)
mysql> insert into t_bluerosehero values (1,20);
Query OK, 1 row affected (0.06 sec)
mysql> insert into t_bluerosehero values (2,40);
ERROR 1146 (42S02): Table 'csdn.xxxx' doesn't exist
mysql>
本文MySQL在触发器里中断记录的插入或更新?到此结束。态度决定命运,细节决定成败。生活方式的恰当与否直接关系到个人所从事工作的顺逆。事业是前台的表演,生活是后台的运作。只有健康体面的生活才能催发蓬勃的生命之树,才能谱写出绚丽多姿的生命圆舞曲。小编再次感谢大家对我们的支持!