您好,登錄后才能下訂單哦!
1、觸發事件:即在何種情況下觸發TRIGGER。
2、觸發時間:即該TRIGGER是在觸發事件發生之前(BEFORE)還是之后(AFTER)觸發。
3、觸發器本身:即該TRIGGER被觸發之后的目的和意圖,正是觸發器本省要做的事情。
4、觸發頻率:說明觸發器內定義的動作被執行的次數。
示例:每次執行刪除操作之后,都會信息提示:“這是刪除操作!”
SQL> --當對emp01表執行刪除操作之后,它就會出現一個提示信息,提示:這是刪除操作
SQL> CREATE TRIGGER first_trigger
2 AFTER DELETE
3 ON emp01
4 BEGIN
5 DBMS_OUTPUT.put_line('這是刪除操作!');
6 END;
7 /
Trigger created
SQL> SET SERVEROUTPUT ON
SQL> DELETE FROM emp01 WHERE empno = 7782;
這是刪除操作!
1 row deleted
CREATE [OR REPLACE] TRIGGER 觸發器的名稱
[BEFORE | AFTER | INTEAD OF ] [DDL事件] ON [DATABASE | SCHEMA]
[WHEN 觸發條件]
[DECLARE]
[程序的聲明部分;]
BEGIN
程序的代碼部分
END;
/
禁止scott用戶的DDL操作
SQL> CREATE OR REPLACE TRIGGER scott_trigger
2 BEFORE DDL
3 ON SCHEMA
4 BEGIN
5 RAISE_APPLICATION_ERROR(-20005, 'scott用戶禁止所有的DDL操作');
6 END;
7 /
Trigger created
SQL> CREATE SEQUENCE test_seq;
CREATE SEQUENCE test_seq
ORA-00604: 遞歸 SQL 級別 1 出現錯誤
ORA-20005: scott用戶禁止所有的DDL操作
ORA-06512: 在 line 2
RAISE_APPLICAITON_ERROR(error_number_in IN NUMBER, error_msg_in IN VARCHAR2);
-- error_msg_in的長度不能超過2k,超過2k后會進行截取
分解成三個步驟:
1、創建數據庫對象DDL操作日志記錄表;2、創建實現對數據庫對象DDL操作記錄的觸發器;3、測試。
SQL> connect system/02000059 as sysdba;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as system AS SYSDBA
SQL> CREATE TABLE object_log( --創建數據庫對象DDL操作日志記錄表
2 logid number constraint pk_logid primary key,
3 operatedate date not null,
4 objecttype varchar2(50) not null,
5 objectowner varchar2(50) not null);
Table created
SQL> CREATE SEQUENCE object_log_seq;
Sequence created
SQL> CREATE OR REPLACE TRIGGER ojbect_trigger --創建實現對數據庫對象DDL操作記錄的觸發器
2 AFTER CREATE OR DROP OR ALTER
3 ON DATABASE
4 BEGIN
5 INSERT INTO object_log(logid, operatedate, objecttype, objectowner) VALUES (object_log_seq.nextval, sysdate, ora_dict_obj_type, ora_dict_obj_owner);
6 END;
7 /
Trigger created
SQL> CREATE SEQUENCE test_seq; --測試
Sequence created
SQL> select * from object_log;
LOGID OPERATEDATE OBJECTTYPE OBJECTOWNER
---------- ----------- -------------------------------------------------- --------------------------------------------------
1 2017/12/21 SEQUENCE SYS
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER}
{DELETE | INSERT | UPDATE [ OF 列名]}
ON 表名
[FOR EACH ROW [WHEN (條件)])
PLSQL塊
分析:1、使用to_char(sysdate, 'day')函數;2、采用語句觸發器。
SQL> CREATE OR REPLACE TRIGGER emp_trigger
2 BEFORE INSERT OR UPDATE OR DELETE
3 ON emp
4 BEGIN
5 IF to_char(sysdate, 'day') IN ('星期六', '星期日') THEN
6 RAISE_APPLICATION_ERROR(-20006, '不能在休息日改變員工信息!');
7 END IF;
8 END;
9 /
Trigger created
SQL> DELETE FROM emp WHERE empno = 7369;
DELETE FROM emp WHERE empno = 7369
ORA-20006: 不能在休息日改變員工信息!
ORA-06512: 在 "SCOTT.EMP_TRIGGER", line 3
ORA-04088: 觸發器 'SCOTT.EMP_TRIGGER' 執行過程中出錯
使用SQL WINDOW窗口:(逐步執行)
--創建審計表
CREATE TABLE delete_emp_audit(
name VARCHAR2(10),
delete_time DATE
);
--創建觸發器
CREATE OR REPLACE TRIGGER del_emp_trigger
AFTER DELETE ON emp
FOR EACH ROW
BEGIN
INSERT INTO delete_emp_audit VALUES(:old.ename, SYSDATE); --插入被刪除員工的姓名和當前時間
END;
--測試
DELETE FROM emp WHERE empno = 7499;
SELECT * FROM delete_emp_audit;
示例:比如要求員工漲后工資不能低于原來的工資,并且所漲的工資不能超過原工資的50%。
SQL> CREATE OR REPLACE TRIGGER tr_check_sal
2 BEFORE UPDATE OF sal ON emp
3 FOR EACH ROW
4 WHEN (new.sal < old.sal OR new.sal > old.sal * 1.5)
5 BEGIN
6 RAISE_APPLICATION_ERROR(-20028, '工資直升不降,并且升幅不能超過50%');
7 END;
8 /
Trigger created
SQL> UPDATE emp SET sal = sal * 1.8 WHERE empno = 7902;
UPDATE emp SET sal = sal * 1.8 WHERE empno = 7902
ORA-20028: 工資直升不降,并且升幅不能超過50%
ORA-06512: 在 "SCOTT.TR_CHECK_SAL", line 2
ORA-04088: 觸發器 'SCOTT.TR_CHECK_SAL' 執行過程中出錯
示例:級聯更新DEPT表的主鍵以及EMP表的外部鍵列。
CREATE OR REPLACE TRIGGER upd_cascade_trigger
AFTER UPDATE OF deptno
ON dept
FOR EACH ROW
BEGIN
UPDATE emp SET deptno = :new.deptno WHERE deptno = :old.deptno;
END;
--測試
UPDATE dept SET deptno = 50 WHERE deptno = 10;
SELECT deptno , ename FROM emp WHERE deptno = 50;
替代觸發器必須包含FOR EACH ROW選項。
1、創建emp_dept視圖:
SQL> CREATE OR REPLACE VIEW emp_dept
2 AS
3 SELECT d.deptno, d.dname, e.empno, e.ename
4 FROM dept d, emp e
5 WHERE d.deptno = e.deptno;
View created
2、當沒有創建替代觸發器時,對emp_dept視圖插入數據出錯:
SQL> INSERT INTO emp_dept VALUES(50, 'DEVELOPMENT', 2222, 'ALICE');
INSERT INTO emp_dept VALUES(50, 'DEVELOPMENT', 2222, 'ALICE')
ORA-01779: 無法修改與非鍵值保存表對應的列
3、創建emp_dept視圖(復雜視圖)的替代觸發器:
CREATE OR REPLACE TRIGGER instead_of_trigger
INSTEAD OF
INSERT
ON emp_dept
FOR EACH ROW
DECLARE
v_temp INT;
BEGIN
SELECT COUNT(*) INTO v_temp FROM dept WHERE deptno = :new.deptno;
IF v_temp = 0 THEN
INSERT INTO dept(deptno, dname) VALUES(:new.deptno, :new.dname);
END IF;
SELECT COUNT(*) INTO v_temp FROM emp WHERE empno = :new.empno;
IF v_temp = 0 THEN
INSERT INTO emp(empno, ename, deptno) VALUES(:new.empno, :new.ename, :new.deptno);
END IF;
END;
4、對emp_dept視圖進行插入操作:
SQL> INSERT INTO emp_dept VALUES(50, 'DEVELOPMENT', 2222, 'ALICE');
1 row inserted
SQL> SELECT * FROM EMP WHERE empno = 2222;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
2222 ALICE 50
SQL> SELECT *FROM DEPT WHERE deptno = 50;
DEPTNO DNAME LOC
------ -------------- -------------
50 ACCOUNTING NEW YORK
SQL> SELECT * FROM emp_dept WHERE empno = 2222;
DEPTNO DNAME EMPNO ENAME
------ -------------- ----- ----------
50 ACCOUNTING 2222 ALICE
系統事件是指與例程或者方案相關的數據庫事件,它包括STARTUP、SHUTDOWN、DB_ROLE_CHANGE和SERVERERROR的四種事件。
1、STARTUP事件觸發器是在啟動數據庫后觸發。
2、SHUTDOWN事件觸發器在關閉數據之前觸發。
3、DB_ROLE_CHANGE事件觸發器在改變角色后第一次打開數據庫時觸發。
4、SERVERERROR事件觸發器在發生Oracle錯誤時觸發。
1、創建記錄發生的數據庫系統EVENT_TALBE事件表:
CREATE TABLE event_table(
event VARCHAR2(50),
event_time date
);
2、創建系統觸發器:
SQL> CREATE OR REPLACE TRIGGER startup_trigger
2 AFTER STARTUP ON DATABASE
3 BEGIN
4 INSERT INTO event_table VALUES(ORA_SYSEVENT, SYSDATE);
5 END;
6 /
Trigger created
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。