下面是一种实现的方法。思路就是想办法在触发器中利用一个出错的语句来中断代码的执行。
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 插入记录时检查记录是否已经存在,存在则更新,不存在则插入记录SQL
- mysql插入和更新时自动更新为当前时间
- python往mysql数据库中写入数据和更新插入数据
- MySql 如何实现无则插入有则更新
- mysql 如果数据不存在,则插入新数据,否则更新的实现方法
- MySQL:数据存在则更新,不存在则插入
- 请教mysql一个sql语句,存在则更新,不存在则插入
- MYSQL主键存在则更新,不存在则插入的解决方案(ON DUPLICATE KEY UPDATE)
- 阿里云部署,ubuntu, 连接服务器 |更新源| 安装node |安装mysql
- MySQL 实现将一个库表里面的数据实时更新到另一个库表里面