程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> MySQL 在觸發器裡中斷記錄的插入或更新?

MySQL 在觸發器裡中斷記錄的插入或更新?

編輯:關於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>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved