乙個簡單MySQL觸發器例子

2021-06-15 04:02:51 字數 1415 閱讀 9741

有這樣乙個需求,更新某張表的某個欄位時,要先判斷,如果新值比表中老值小,則將老值和新值相加,然後更新;否則正常更新。考慮用mysql的觸發器實現,更新時觸發。

下面是具體的sql, 一看便知。

-- 刪除觸發器

drop trigger trigger_ads;

-- 建立觸發器

delimiter //

create trigger trigger_ads before update on stats_ads

for each row

begin

if old.view > new.view then

set new.view = old.view + new.view;

end if;

if old.view_unique > new.view_unique then

set new.view_unique = old.view_unique + new.view_unique;

end if;

if old.click > new.click then

set new.click = old.click + new.click;

end if;

if old.click_unique > new.click_unique then

set new.click_unique = old.click_unique + new.click_unique;

end if;

if old.start > new.start then

set new.start = old.start + new.start;

end if;

if old.start_unique > new.start_unique then

set new.start_unique = old.start_unique + new.start_unique;

end if;

if old.landing > new.landing then

set new.landing = old.landing + new.landing;

end if;

if old.landing_unique > new.landing_unique then

set new.landing_unique = old.landing_unique + new.landing_unique;

end if;

end;

//

最後可以通過下面sql語句檢視所有觸發器:

-- 檢視mysql觸發器

select * from information_schema.`triggers`;

Oracle觸發器的乙個簡單例子

使用者狀態變成離職狀態,系統配置表被觸發,修改配置表狀態 create or replace trigger basedb userleft before update or delete on basedb users for each row declare local variables he...

乙個觸發器的例子

create or replace trigger usremhr trg sq rule insert before insert on sq rule base for each row declare temp rule id varchar2 10 temp rul theme id var...

乙個Oracle觸發器的例子

有乙個表,表名是xx,有abcd四個字段,正常情況下,abc abd這三個欄位都可以唯一確定一條記錄,按理應該做成唯一索引,但由於歷史原因,該錶存在重複資料,但要刪掉哪一條需要人工判斷,無法用語句批量刪除,於是唯一索引加不上。但為了保證以後資料的準確性,需要控制新插進去的記錄是唯一的。於是我寫了乙個...