触发器
创建触发器时,需要给出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; //