Home > Database > Mysql Tutorial > MySQL触发器trigger学习_MySQL

MySQL触发器trigger学习_MySQL

WBOY
Release: 2016-06-01 13:04:10
Original
853 people have browsed it

触发器:一类特殊的事物,可监视某种数据操作,并触发相关操作(insert/update/delete);表中的某些数据改变,希望同时可以引起其它相关数据改变的需求。 作用:变化自动完成某些语句查询,添加程序的灵活性。 创建触发器语法: #delimiter $$ //默认情况下,delimiter是分号“;”,sql语句带有;号结尾会报错,没到end就算到结束了,使用delimiter $$作用就是告诉mysql语句的结尾换成以$结束,相应使用end$$结束

create trigger test1  #触发器名称
(after/before) #触发时间
(insert/update/delete)  #监视事件
on table #监视地点(表名)
for each row  #mysql必须加的 
begin
    sql1
    ...
    sqlN
end;
#end$$
Copy after login
查询已有trigger:show triggers 删除已有trigger:drop trigger 触发器名称 new/old: update触发:old 代表更新前的记录。new 代表更新后的记录. insert触发:old 不能使用。new 代表插入的记录. delete触发:old 代表删除的记录。new 不能使用.
实例操作: goods商品表和ord订单表
CREATE TABLE goods(
`goods_id` INT(10),
`name` VARCHAR(20),
`num` SMALLINT(4)
)ENGINE=INNODB CHARSET=utf8
CREATE TABLE `ord`(
`oid` INT(10),
`gid` INT(10),
`much` INT(10)
)ENGINE=INNODB CHARSET=utf8
INSERT INTO goods VALUES
(1, 'cat', 26),(2, 'dog', 26),(3, 'pig', 26);
Copy after login
1、要求每下一个订单,goods表中的库存相应减少订单的数量。 mysql> select * from goods +----------+------+------+ | goods_id | name | num | +----------+------+------+ | 1 | cat | 26 | | 2 | dog | 26 | | 3 | pig | 26 | +----------+------+------+ 3 rows in set (0.00 sec) mysql> select * from ord; Empty set (0.00 sec)
#创建触发器test1
DELIMITER $$
CREATE TRIGGER test1
AFTER
INSERT
ON `ord`
FOR EACH ROW
BEGIN
UPDATE goods SET num= num - new.much WHERE goods_id = new.gid;
END$$
Copy after login

mysql> CREATE TRIGGER test1 -> AFTER -> INSERT -> ON `ord` -> FOR EACH ROW -> BEGIN -> UPDATE goods SET num= num - new.much WHERE goods_id = new.gid; -> END$$ Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO `ord` VALUES (1, 2, 2)$$ Query OK, 1 row affected (0.03 sec) mysql> select * from ord$$ +------+------+------+ | oid | gid | much | +------+------+------+ | 1 | 2 | 2 | +------+------+------+ 1 row in set (0.00 sec) mysql> select * from goods$$ +----------+------+------+ | goods_id | name | num | +----------+------+------+ | 1 | cat | 26 | | 2 | dog | 24 | | 3 | pig | 26 | +----------+------+------+ 3 rows in set (0.00 sec) goods表里面的dog由26变成24
2、用户取消订单后商品库存订单的商品数要正常入库。以下只写触发器trigger,操作过程不写太多比较乱。
#触发器test2
CREATE TRIGGER test2
AFTER
DELETE
ON `ord`
FOR EACH ROW
BEGIN
UPDATE goods SET num = num + old.much WHERE goods_id = old.gid;
END $$ 
Copy after login
\
3、用户更新订单的时候,商品库存要根据订单的数量正常更新。
#触发器test3
CREATE TRIGGER test3
AFTER
UPDATE
ON `ord`
FOR EACH ROW
BEGIN
UPDATE goods SET num = num + old.much - new.much WHERE goods_id = new.gid;
END$$
Copy after login
\

查询触发器 \
删除触发器


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template