标签:
CREATE [OR REPLACE] TRIGGER trigger_name AFTER | BEFORE | INSTEAD OF [INSERT] [[OR] UPDATE [OF column_list]] [[OR] DELETE] ON table_or_view_name [REFERENCING {OLD [AS] old / NEW [AS] new}] [FOR EACH ROW] [WHEN ( condition ) ] pl/sql_block;
:new --为一个引用最新的列值;
:old --为一个引用以前的列值;
这两个变量只有在使用了关键字
"FOR
EACH ROW"时才存在.
且update语句两个都有,而insert只有:new ,delect 只有:old;
CREATE OR REPLACE TRIGGER trig_sal AFTER UPDATE OF empsal ON salary_records --在更新 emp_sal 列之后激活触发器 …
… FOR EACH ROW WHEN (NEW.empsal>OLD.empsal) --只有在WHEN子句中的条件得到满足时,才激活trig_sal 触发器 DECLARE Sal_diff NUMBER; …
… BEGIN sal_diff:=:NEW.empsal-:OLD.empsal; --如果WHEN子句中的条件得到满足,将执行BEGIN 块中的代码 DBMS_OUTPUT.PUT_LINE(‘工资差额:’sal_diff); END;
CREATE OR REPLACE TRIGGER aiu_itemfile AFTER INSERT ON itemfile FOR EACH ROW BEGIN IF (:NEW.qty_hand = 0) THEN DBMS_OUTPUT.PUT_LINE(‘警告:已插入记录,但数量为零‘); ELSE DBMS_OUTPUT.PUT_LINE(‘已插入记录‘); END IF; END;
查看表的触发器
select * from all_triggers where table_name =upper(‘tbname‘)
CREATE OR REPLACE TRIGGER TR_SEC_EMP BEFOR INSERT OR UPDATE OR DELETE ON EMP2 BEGIN IF TO_CHAR(SYSDATE,’DY’, ‘nls_date_language=AMERICAN’) IN (‘SAT’, ‘SUN’) THEN RAISE_APPLICATION_ERROR(-20002,’禁止修改数据!’); END IF; END;
create or replace trigger guo_trigger----创建触发器 before update on emp2 ----指明触发器时机 for each row ----行触发器标识 when (new.sal<old.sal ) ----触发条件 begin raise_application_error(-20500,‘不能给员工减少工资‘); end;
语句级(STATEMENT)触发器:是指当某触发事件发生时,该触发器只执行一次;行级(ROW)触发器:是指当某触发事件发生时,对受到该操作影响的每一行数据,触发器都单独执行一次。
l 不同类型的触发器(如DML触发器、INSTEAD OF触发器、系统触发器)的语法格式和作用有较大区别。
CREATE TABLE TEST_TRG (ID NUMBER, NAME VARCHAR2(20)); CREATE SEQUENCE SEQ_TEST;
CREATE OR REPLACE TRIGGER BI_TEST_TRG BEFORE INSERT OR UPDATE OF ID ON TEST_TRG FOR EACH ROW BEGIN --函数UPDATING, DELETING判断触发器是由哪个操作触发的 IF INSERTING THEN SELECT SEQ_TEST.NEXTVAL INTO :NEW.ID FROM DUAL; ELSE RAISE_APPLICATION_ERROR(-20020, ‘不允许更新ID值!‘); END IF; END;
修改数据前备份员工工资
CREATE OR REPLACE TIGGER TR_SAL_BACK AFFTER UPDATE OF SAL ON EMP2 FOR EACH ROW DECLARE V_TEM INT ; BEGIN SELECT COUNT(*) INTO V_TEMP FROM NEW_BACK WHERE ENAME =:OLD.ENAME; IF V_TEMP =0 THEN INSERT INTO NEW_BACK VALUES (:OLD.ENAME,:OLD.SAL, :NEW.SAL,SYSDATE); ELSE UPDATE NEW_BACK VALUES SET OLDSAL=:OLDSAL, NEWSAL=:NEW.SAL,TIME=SYSDATE WHERE NAME=:OLD.ENAME; END IF; END;
建立一个触发器, 当职工表 emp 表被删除一条记录时,把被删除记录写到职工表删除日志表中去。
CREATE TABLE emp_his AS SELECT * FROM EMP WHERE 1=2; CREATE OR REPLACE TRIGGER tr_del_emp BEFORE DELETE --指定触发时机为删除操作前触发 ON scott.emp FOR EACH ROW --说明创建的是行级触发器 BEGIN --将修改前数据插入到日志记录表 del_emp ,以供监督使用。 INSERT INTO emp_his(deptno , empno, ename , job ,mgr , sal , comm , hiredate ) VALUES( :old.deptno, :old.empno, :old.ename , :old.job,:old.mgr, :old.sal, :old.comm, :old.hiredate ); END; DELETE emp WHERE empno=7788; DROP TABLE emp_his; DROP TRIGGER del_emp;
CREATE OR REPLACE TRIGGER trgdemo AFTER INSERT OR UPDATE OR DELETE ON order_master BEGIN IF UPDATING THEN DBMS_OUTPUT.PUT_LINE(‘已更新 ORDER_MASTER 中的数据‘); ELSIF DELETING THEN DBMS_OUTPUT.PUT_LINE(‘已删除 ORDER_MASTER 中的数据‘); ELSIF INSERTING THEN DBMS_OUTPUT.PUT_LINE(‘已在 ORDER_MASTER 中插入数据‘); END IF; END;
CREATE OR REPLACE TRIGGER upd_ord_view INSTEAD OF UPDATE ON ord_view FOR EACH ROW BEGIN UPDATE order_master SET vencode=:NEW.vencode WHERE orderno = :NEW.orderno; DBMS_OUTPUT.PUT_LINE(‘已激活触发器‘); END;
CREATE TABLE dropped_obj
( obj_name VARCHAR2(30), obj_type VARCHAR2(20), drop_date DATE
);
CREATE OR REPLACE TRIGGER log_drop_obj AFTER DROP ON SCHEMA BEGIN INSERT INTO dropped_obj VALUES( ORA_DICT_OBJ_NAME, ORA_DICT_OBJ_TYPE, SYSDATE); END;
ALTER TRIGGER aiu_itemfile DISABLE;
ALTER TRIGGER aiu_itemfile ENABLE;
DROP TRIGGER aiu_itemfile;
SELECT TRIGGER_NAME FROM USER_TRIGGERS WHERE TABLE_NAME=‘EMP‘;
SELECT TRIGGER_TYPE, TRIGGERING_EVENT, WHEN_CLAUSE FROM USER_TRIGGERS WHERE TRIGGER_NAME = ‘BIU_EMP_DEPTNO‘;
标签:
原文地址:http://www.cnblogs.com/hopeblog/p/5003822.html