1.DELETE TRIGGER,used for delete employer after delete the department. This is a rows trigger.
CREATE OR REPLACE TRIGGER del_eptid
AFTER DELETE ON deptment
FOR EACH ROW--Rows trigger
BEGIN
DELETE FROM emp WHERE id=:old.id;
END del_deptid;
/
2.INSERT TRIGGER,used for insert a new employer after create a new department.This is a rows trigger.
CREATE OR REPLACE TRIGGER insert_dept
AFTER INSERT ON deptment
FOR EACH ROW
BEGIN
INSERT INTO emp(eid,ename,id) values('121','QWERT',:new.id);
END
;
/
3.UPDATE Trigger. Used for update employer's department after update the department id.
CREATE OR REPLACE TRIGGER update_dept
AFTER UPDATE ON deptment
FOR EACH ROW --Rows trigger.
BEGIN
UPDATE emp SET id=:new.id WHERE id=:old.id;
END;
/
4.DELETE TRIGGER,for a special row,not allowed to delete,raise a error.
CREATE OR REPLACE TRIGGER book_delete
AFTER DELETE ON books
FRO EACH ROW
BEGIN
IF :old.books_id='0001' THEN
RAISE_APPLICATION_ERROR(-20000,'Not allow delete!');
END IF;
END;
/
5.语句级触发器。当一个操作发生时,记录相关操作。
CREATE OR REPLACE TRIGGER dml_aa
AFTER INSERT OR DELETE OR UPDATE ON aa
BEGIN
IF INSERTING THEN
INSERT INTO mylog VALUES(user,sysdate,'I');
ELSEIF DELETING THEN
INSERT INTO mylog VALUES(user,sysdate,'D');
ELSE
INSERT INTO mylog VALUES(user,sysdate,'U');
END IF;
END;
/
6.ROWS TRIGGER.
--此事例用于给AUTO表的NUMBER列一数字。
CREATE OR REPLACE TRIGGER set_no
BEFORE INSERT ON auto
FOR EACH ROW
DECLARE
sn number(5);
BEGIN
SELECT myseq.nextval INTO sn FROM dual;
:NEW.a:=sn;
END;
/
7.--替换触发器
--解决视图多表更新问题,而且替换触发器只能建立于视图之上
--SHOW ERROR 用于显示触发器的错误
CREATE OR REPLACE TRIGGER tr_v_e_d
INSTEAD OF INSERT ON v_emp_dept
FOR EACH ROW
BEGIN
INSERT INTO deptment VALUES(:new.id,:new.name);
INSERT INTO emp(edi,ename,sex,id) VALUES(:new.eid,:new.ename,:new.sex,:new.id);
END;
/