日期:2014-05-16 浏览次数:20394 次
CREATE OR REPLACE TRIGGER trigger_name Timing event1[OR event2OR event3] ON table_name Trigger_body举例1:
CREATE OR REPLACE TRIGGER secure_person BEFORE INSERT ON person BEGIN IF (TO_CHAR(SYSDATE,’DY’) IN (’SAT’,’SUN’)) OR (TO_CHAR(SYSDATE,’HH24:MI’) NOT BETWEEN ’08:00’AND ’18:00’) THEN RAISE_APPLICATION_ERROR (-20500,’You may insert into personS table onlyduring business hours.’); END IF; END; /举例2:
CREATE OR REPLACE TRIGGER secure_person BEFORE INSERT OR UPDATE OR DELETE ON person BEGIN IF (TO_CHAR (SYSDATE,’DY’) IN (’SAT’,’SUN’)) OR(TO_CHAR (SYSDATE, ’HH24’) NOT BETWEEN ’08’AND ’18’)THEN IF DELETING THEN RAISE_APPLICATION_ERROR (-20502,’You may delete from person table only during business hours.’); ELSIF INSERTING THEN RAISE_APPLICATION_ERROR (-20500,’You may insert into person table only during business hours.’); ELSIF UPDATING (‘SALARY’) THEN RAISE_APPLICATION_ERROR (-20503,’You may update SALARY only during business hours.’); ELSE RAISE_APPLICATION_ERROR (-20504,’You may Update person table only during normal hours.’); END IF; END IF; END; /
CREATE[OR REPLACE] TRIGGER trigger_name timing event1 [OR event2 OR event3] ON table_name [REFERENCING OLD AS old| NEW AS new] FOR EACH ROW [WHEN (condition)] Trigger_body举例1:
CREATE OR REPLACE TRIGGER restrict_age BEFORE INSERT OR UPDATE OF age ON person FOR EACH ROW BEGIN IF NOT (:NEW.per_id IN (’AD_PRES’, ’AD_VP’)) AND :NEW.age > 25 THEN RAISE_APPLICATION_ERROR (-20202,’person can not in this amount’); END IF; END; /
CREATE OR REPLACE TRIGGER audit_emp_values AFTER DELETE OR INSERT OR UPDATE ON employees FOR EACH ROW BEGIN INSERT INTO audit_emp_table (user_name, timestamp, id, old_last_name, new_last_name, old_title, new_title, old_salary, new_salary) VALUES (USER, SYSDATE, :OLD.employee_id, :OLD.last_name, :NEW.last_name, :OLD.job_id, :NEW.job_id, :OLD.salary, :NEW.salary ); END; /OLD 和 NEW 限定词只在行触发器中可用。在每一个 SQL 和 PL/SQL 语句中用冒号 (:) 前缀这些限定词。如果这些限定词在 WHEN 限制条件中被引用则无冒号 (:) 前缀。