当前位置: 首页 > 图文教程 > 数据库 > MYSQL > MySQL 在触发器里中断记录的插入或更新?

MYSQL
图片通过asp.net上传到mysql数据库的方法
mysql教程之存储
用来分析、监控和变换通信数据MySQL-Proxy
MySQL教程:相关的环境变量
MySQL新功能测试
MySQL数据库规范化设计
MySQL故障检测和修正的一般过程
写给菜鸟站长:解读你的茫然
如何取得MYSQL中ENUM列的全部可能值
mysql基础教程:安装与操作
phpMyAdmin安装方法及介绍
MySQL:改善数据装载效率的策略分析
为MySQL数据库换挡加速
MySQL参数是如何设置的
mysql中如何随机提取数据库记录
Windows下MySQL安全权限设置
WordPress基础,让你全面了解WordPress
Photoshop教程:绘制著名博客程序的LOGO
PHP通过SQL语句将数据写入MySQL数据库指定的表
Six6免费空间,6G容量,无限月流量

MYSQL 中的 MySQL 在触发器里中断记录的插入或更新?


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 110 ::
收藏到网摘: n/a

MySQL 不象其它有些数据库可以在触发器中抛出异常来中断当然触发器的执行以阻止相应的SQL语句的执行。在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>