Mysql基础第二十八天,使用触发器
Posted 2019ab
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql基础第二十八天,使用触发器相关的知识,希望对你有一定的参考价值。
触发器
创建触发器时,需要给出4条信息
delimiter //
create trigger trg_order_inser after insert on orders
for each row
begin
insert into order_log(o_date,order_num) values (now(),new.order.num);
end;
//
使用触发器
create table order_log(id int auto_increment primary key,o_date datetime,order_num int);
drop trigger trg_order_inser;
create trigger trg_order_inser after insert on orders
for each row
begin
insert into order_log(o_date,order_num,dml_type) values (now(),new.order.num,'insert');
end;
insert into orders (order_date,cust_id) values('2021-1-1',10005);
删除触发器
create trigger trg_order_delete after delete on orders
for each row
begin
insert into order_log(o_date,order_num,dml_type) values (now(),old.order.num,'delete');
end;
delete from orders where order_num = 2010;
更新触发器
create trigger trg_order_update after update on orders
for each row
begin
insert into order_log(o_date,order_num,dml_type) values (now(),old.order.num,'update');
end; // update 操作
delimiter //
create trigger trg_products_update before update on product
for each row
begin
if new.prod_price>old.prod_price * 1.2 then
set new.prod_price = old.prod_price *1.2;
end if;
end;
//
以上是关于Mysql基础第二十八天,使用触发器的主要内容,如果未能解决你的问题,请参考以下文章