MySQL 在触发器里中断记录的插入或更新?
网络编程 2021-07-05 15:27www.168986.cn编程入门
MySQL 不象其它有些数据库可以在触发器中抛出异常来中断触发器的执行以阻止相应的SQL语句的执行。在MySQL的目录版本中还无法直接抛出异常。这样我们如何实现呢?
狼蚁网站SEO优化是一种实现的方法。思路就是想办法在触发器中利用一个出错的语句来中断代码的执行。
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> 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>
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程