裡面主要說的是同乙個位址2個不同資料庫之前的操作,a資料庫裡面a表資料有 新增、修改、刪除時,去觸發下面的函式,然後對b資料庫裡面的b表做對應的操作(a表b表結構可以說是相同的)
drop trigger if exists `add_rma_order`;
delimiter ;;
create trigger `add_rma_order` after insert on `tp_user_rma_order_descript` for each row begin
declare oid int default 0;
declare path varchar(100) default '';
select rma.id into oid from ptoa.rma_order_descript as rma
where rma.order_id=new.order_id;
if(oid>0) then
update ptoa.rma_order_descript as rma set
rma.uid=(select oa_id from tp_user_info where uid=new.uid),
rma.addr_front=new.addr_front,rma.addr_last=new.addr_last,rma.do_addr_front=new.do_addr_front,rma.do_addr_last=new.do_addr_last,
rma.tel=new.tel,rma.bill_img=concat(path,new.bill_img),rma.bank_img=concat(path,new.bank_img),rma.do_supplier=new.do_supplier,rma.to_time=new.to_time,
rma.free_date=new.free_date,rma.export_bill=concat(path,new.export_bill),rma.in_supplier=new.in_supplier,
rma.brand_img=concat(path,new.brand_img),
rma.product_img=concat(path,new.product_img),rma.ex_time=new.ex_time,rma.goods_hs_code=new.goods_hs_code,
rma.declare_element=new.declare_element,rma.owner=(select staff_name from tp_user_info where uid=new.uid),rma.udate=unix_timestamp()
where rma.order_id=new.order_id;
else
insert into ptoa.rma_order_descript(uid,order_id,addr_front,addr_last,do_addr_front,do_addr_last,
tel,bill_img,bank_img,do_supplier,to_time,free_date,export_bill,in_supplier,brand_img,product_img,
ex_time,goods_hs_code,declare_element,owner,cdate)
values((select oa_id from tp_user_info where uid=new.uid),new.order_id,
new.addr_front,new.addr_last,new.do_addr_front,new.do_addr_last, new.tel,concat(path,new.bill_img),concat(path,new.bank_img),
new.do_supplier,
new.to_time,new.free_date,concat(path,new.export_bill),new.in_supplier,concat(path,new.brand_img),concat(path,new.product_img),
new.ex_time,new.goods_hs_code,new.declare_element,(select staff_name from tp_user_info where uid=new.uid),unix_timestamp());
end if;
end;;
delimiter ;
drop trigger if exists `edit_rma_order`;
delimiter ;;
create trigger `edit_rma_order` after update on `tp_user_rma_order_descript` for each row begin
declare oid int default 0;
declare path varchar(100) default '';
select rma.id into oid from ptoa.rma_order_descript as rma
where rma.order_id=old.order_id;
if(oid>0) then
update ptoa.rma_order_descript as rma set
rma.uid=(select oa_id from tp_user_info where uid=old.uid),
rma.addr_front=new.addr_front,rma.addr_last=new.addr_last,rma.do_addr_front=new.do_addr_front,rma.do_addr_last=new.do_addr_last,
rma.tel=new.tel,rma.bill_img=concat(path,new.bill_img),rma.bank_img=concat(path,new.bank_img),rma.do_supplier=new.do_supplier,rma.to_time=new.to_time,
rma.free_date=new.free_date,rma.export_bill=concat(path,new.export_bill),rma.in_supplier=new.in_supplier,rma.brand_img=concat(path,new.brand_img),
rma.product_img=concat(path,new.product_img),rma.ex_time=new.ex_time,rma.goods_hs_code=new.goods_hs_code,rma.declare_element=new.declare_element,
rma.owner=(select staff_name from tp_user_info where uid=new.uid),rma.udate=unix_timestamp()
where rma.order_id=old.order_id;
else
insert into ptoa.rma_order_descript(uid,order_id,addr_front,addr_last,do_addr_front,do_addr_last,
tel,bill_img,bank_img,do_supplier,to_time,free_date,export_bill,in_supplier,brand_img,product_img,
ex_time,goods_hs_code,declare_element,owner,cdate)
values((select oa_id from tp_user_info where uid=old.uid),new.order_id,
new.addr_front,new.addr_last,new.do_addr_front,new.do_addr_last, new.tel,concat(path,new.bill_img),concat(path,new.bank_img),new.do_supplier,
new.to_time,new.free_date,concat(path,new.export_bill),new.in_supplier,concat(path,new.brand_img),concat(path,new.product_img),
new.ex_time,new.goods_hs_code,new.declare_element,(select staff_name from tp_user_info where uid=new.uid),unix_timestamp());
end if;
end;;
delimiter ;
drop trigger if exists `del_rma_order`;
delimiter ;;
create trigger `del_rma_order` after delete on `tp_user_rma_order_descript` for each row begin
declare oid int default 0;
select rma.id into oid from ptoa.rma_order_descript as rma
where rma.order_id=old.order_id;
if(oid>0)
then
update ptoa.rma_order_descript as rma set
rma.del_author=(select oa_id from tp_user_info where uid=old.uid),rma.del_date=now(),rma.del_state='已刪除'
where rma.order_id=old.order_id;
end if;
end;;
delimiter ;
積分觸發器 mysql 觸發器的使用
將多個資料庫中的某列資料同步時需要用到mysql觸發器,以下可做參考,已簡單測試ok.修改紅棗的積分時觸發 drop trigger if exists u hongzaocount delimiter create trigger u hongzaocount after update on ho...
使用觸發器
觸發器 trigger 是個特殊的儲存過程 一般儲存過程主要通過儲存過程名而被直接呼叫,觸發器則是通過事件觸發執行。觸發器基於乙個表來建立並和乙個或多個資料修改操作 插入 更新或刪除 相關聯,可視作表的一部分。觸發器與資料庫中的表緊密相關,比如當對表執行insert update或delete操作時...
觸發器的使用!
可以定義乙個無論何時用insert語句向表中插入資料時都會執行的觸發器。當觸發insert觸發器時,新的資料行就會被插入到觸發器表和inserted表中。inserted表是乙個邏輯表,它包含了已經插入的資料行的乙個副本。inserted表包含了insert語句中已記錄的插入動作。inserted表...