oracle 資料庫觸發器的使用

2021-04-21 07:32:04 字數 1843 閱讀 3829

1、插入表觸發器

create or replace trigger "editship" before

insert on "dtsj" for each row

declare mmsi int;

shipinfommsi int;

begin

select max("mmsi") into mmsi from "shipmaterial" s left join "material" m on s."id"=m."id"

where trim(m."name")=trim(:new.ydpth);

if mmsi is null then

select max(mmsi) into mmsi from "shipinfo" where trim("name")=trim(:new.ydpth);

if mmsi is null then

select max(mmsi) into mmsi from "shipinfo";

if mmsi is null then

mmsi:=1;

else

mmsi:=mmsi+1;

end if;

insert into "shipinfo"(mmsi,"name","shiptype","lat","lon","time","cog","sog")

values( mmsi,:new.ydpth,52,:new.wd,:new.jd,:new.sj,:new.fx,:new.sd);

else

update "shipinfo" set "shiptype"=52,"lat"=:new.wd,"lon"=:new.jd,"time"=:new.sj,"cog"=:new.fx,"sog"=:new.sd

where trim(mmsi)=trim(mmsi) and trim("name")=trim(:new.ydpth);

end if;

else

select max(mmsi) into shipinfommsi from "shipinfo" where trim("name")=trim(:new.ydpth);

if shipinfommsi is null then

insert into "shipinfo" (mmsi,"name","shiptype","lat","lon","time","cog","sog")

values( mmsi,:new.ydpth,52,:new.wd,:new.jd,:new.sj,:new.fx,:new.sd);

else

update "shipinfo" set "shiptype"=52,"lat"=:new.wd,"lon"=:new.jd,"time"=:new.sj,"cog"=:new.fx,"sog"=:new.sd

where trim(mmsi)=trim(mmsi) and trim("name")=trim(:new.ydpth);

end if;

end if;

insert into "shiphis"

(mmsi,"lat","lon","time","cog","sog")

values( mmsi,:new.wd,:new.jd,:new.sj,:new.fx,:new.sd);

end;

2、刪除觸發器

create or replace trigger "觸發器名稱(delete子表名by主表id)" before

delete on 主表名 for each row

begin

delete from bb where 子表中主表id=:old.主表主鍵id;

end;

oracle資料庫觸發器

instead of觸發器介紹 instead of觸發器代替觸發動作,並在處理約束之前激發 對於每個觸發操作,每個表或檢視都只能有乙個instead of觸發器。而乙個表對於每個觸發操作可以有多個after觸發器 instead of觸發器的建立模板 create or replace trigg...

Oracle資料庫update觸發器

在專案中使用到oracle資料庫的update觸發器,先記錄下來方便以後查閱。預備知識和格式以後再編輯吧,先把內容貼出來。create or replace trigger demo trigger after update on tb goods referencing new as new ol...

Oracle資料庫觸發器例項

需求 在修改表qdyth.jzfpjcxx時,要實時記錄表中的資料變動情況 協商後結果 建立新錶記錄資料變動情況 實現手段 資料庫觸發器 建立或修改觸發器 tr jzfpjcxx 2 create or replace trigger tr jzfpjcxx 2 觸發條件 before delete...