标签:修改 hang end 查看 订单 triggers dog val database
创建商品表:
Database changed
mysql> create table goods(
-> gid int(10),
-> name varchar(20),
-> num smallint
-> );
Query OK, 0 rows affected (0.02 sec)
创建订单表:
mysql> create table ord(
-> ord int(10),
-> gid int(10),
-> mch smallint,
-> );
插入测试数据:
insert into goods values(1,‘cat‘,34),(2,‘dog‘,65),(3,‘pig‘,21);
mysql> select * from goods;
+------+------+------+
| gid | name | num |
+------+------+------+
| 1 | cat | 34 |
| 2 | dog | 65 |
| 3 | pig | 21 |
+------+------+------+
创建触发器:
//修改mysql的结束符号为$,因为select语句要用分号
delimiter $
mysql> create trigger t1 after insert on ord for each
-> row begin update goods set num = num-2 where gid = 1;
-> end $
Query OK, 0 rows affected (0.02 sec)
查看触发器:
mysql> show triggers;
-> $
现在操作ord数据添加的时候,goods表的数据会跟着减少了
标签:修改 hang end 查看 订单 triggers dog val database
原文地址:https://www.cnblogs.com/ayanboke/p/10965086.html