问下下,取消update的问题

MySql 码拜 8年前 (2016-02-19) 1020次浏览
本人想在触发器中,假如a=1 就取消这条update
例如:update t1 set a=2 where a=1 那么这条update取消
update t1 set a=2 where a=3 这条update通过
create tigger….
这里应该怎么写。
end….
解决方案

20

你用这个试试:

mysql> delimiter $$
mysql> create trigger tri_tb2
    -> before update on tb2 for each row
    -> begin
    -> if new.id<>1 and old.id is not null then
    -> SIGNAL SQLSTATE "HY001" SET MESSAGE_TEXT = "error:拒绝更新";
    -> end if;
    -> end$$
Query OK, 0 rows affected (0.07 sec)
mysql> insert into tb2 values(1,"b");
    -> $$
Query OK, 1 row affected (0.06 sec)
mysql> select * from tb2;
    -> $$
+--+--+
| id   | name |
+--+--+
|    1 | aa   |
|    1 | bb   |
|    1 | cc   |
|    1 | aa   |
|    2 | aa   |
|    2 | bb   |
|    2 | dd   |
|    2 | cc   |
|   10 | a    |
|    1 | a    |
|    2 | a    |
|    1 | b    |
+--+--+
12 rows in set (0.00 sec)
mysql> update tb2 set id = 20 where id = 1;
    -> $$
ERROR 1644 (HY001): error:拒绝更新
mysql> select * from tb2;
    -> $$
+--+--+
| id   | name |
+--+--+
|    1 | aa   |
|    1 | bb   |
|    1 | cc   |
|    1 | aa   |
|    2 | aa   |
|    2 | bb   |
|    2 | dd   |
|    2 | cc   |
|   10 | a    |
|    1 | a    |
|    2 | a    |
|    1 | b    |
+--+--+
12 rows in set (0.00 sec)

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明问下下,取消update的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)