节选自 http://blog.csdn.net/indexman/article/details/8023740/
CREATE OR REPLACE TRIGGER triger_DESP_to_segmentpoint
AFTER --可以选 BEFORE 或 AFTER ,指触发器在附着表操作的之前还是之后触发INSERT OR DELETE --触发事件,可以在 INSERT | DELETE | UPDATE(OF column ...) 中单选或多选,多选只能用OR连接,不能用ADDON DESIGNATED_POINT --附着表,或视图,触发器只能附着在一张表/视图上FOR EACH ROW --FOR EACH ROW选项说明触发器为行触发器。还有可以有语句触发器,二者区别是触发次数以行为单位还是语句为单位--参照当前的新、旧列值,默认的相关名称分别为OLD和NEW。触发器的PL/SQL块中应用相关名称时,必须在它们之前加冒号(:),但在WHEN子句中则不能加冒号。
BEGIN CASE WHEN DELETING THEN --判断动作类型,可以选择 INSERTING|DELETING|UPDATING [(column_1...)更新指定列时触发]:--WHEN 子句说明触发约束条件,WHEN 子句指定的触发约束条件只能用在BEFORE 和AFTER 行触发器中,不能用在INSTEAD OF 行触发器和其它类型的触发器中。
delete from segment_point where point_uuid = :old.designated_point_uuid; WHEN INSERTING THEN insert into segment_point (point_uuid) valueS(:new.Designated_Point_Uuid); --如果是INSERTING,能用:new代表行,而用:old只能返回null值, END CASE;END;实例1:添加/删除类型triger
CREATE OR REPLACE TRIGGER triger_NAVAID_to_segmentpoint
AFTER INSERT OR DELETEON NAVAIDFOR EACH ROWBEGIN CASE WHEN DELETING THEN delete from segment_point where point_uuid = :old.NAVAID_UUID; WHEN INSERTING THEN insert into segment_point (point_uuid) valueS(:new.NAVAID_UUID); END CASE;END;实例2、3有条件的添加trigger
--删除触发器Navaid
CREATE OR REPLACE TRIGGER Navaid_to_sgmtpt_delAfter DeleteON NavaidFor Each RowBegin delete from segment_point where point_uuid = :old.Navaid_uuid;End;/--插入触发器Navaid_tsCREATE OR REPLACE TRIGGER Navaid_to_sgmtpt_insertAfterInsertON Navaid_tsFor Each Row when(new.Type_id<=7 and new.interpretation='BASELINE')Declare v_count Number;Begin select count(*) into v_count from segment_point where point_uuid =:new.Navaid_uuid; if v_count=0 then insert into segment_point (point_uuid) valueS(:new.Navaid_Uuid); end if;End;/