Oracle數據庫---觸發器

SQL> --當咱們對empnew執行刪除操做以後,它就會出現一個提示信息,提示:這是刪除操做!
SQL> CREATE TRIGGER first_trigger
2 AFTER DELETE
3 ON empnew
4 BEGIN
5 DBMS_OUTPUT.put_line('這是刪除的操做!');
6 END;
7 /
Trigger created數據庫

SQL> SET SERVEROUTPUT ON
SQL> DELETE FROM empnew WHERE empno = 7788;
這是刪除的操做!
1 row deleted測試

--案例1:禁止SCOTT用戶的DDL操做
CREATE OR REPLACE TRIGGER scott_trigger
BEFORE DDL
ON SCHEMA
BEGIN
RAISE_APPLICATION_ERROR(-20005,'scott用戶禁止全部的DDL操做!');
END;日誌

--測試
CREATE SEQUENCE test_seq;對象

--案例2:
--建立一個記錄數據庫對象DDL操做的日誌表
create table object_log(
logid number constraint pk_logid primary key,
operatedate date not null,
objecttype varchar2(50) not null,
objectowner varchar2(50) not null
);事件

CREATE SEQUENCE object_log_seq;開發

--建立觸發實現對數據庫對象DDL操做記錄的觸發器
CREATE OR REPLACE TRIGGER object_trigger
AFTER CREATE OR DROP OR ALTER
ON DATABASE
BEGIN
INSERT INTO object_log(logid,operatedate,objecttype,objectowner)
VALUES(object_log_seq.nextval,sysdate,ORA_DICT_OBJ_TYPE,ORA_DICT_OBJ_OWNER);
END;it

--測試
conn yanln/yanln
create sequence test_seq1;event

conn system/password
select * from object_log;table

--開發示例1test

SQL> CREATE OR REPLACE TRIGGER emp_trigger1
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 = 7788;
delete from emp where empno = 7788
ORA-20006: 不能在休息日改變員工信息!
ORA-06512: 在 "SCOTT.EMP_TRIGGER1", line 3
ORA-04088: 觸發器 'SCOTT.EMP_TRIGGER1' 執行過程當中出錯

--開發示例2

--建立審計表
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;

--開發示例3
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 = 7788;
UPDATE emp SET sal = sal*1.8 WHERE empno = 7788
ORA-20028: 工資只升不降,而且升幅不能超過50%
ORA-06512: 在 "SCOTT.TR_CHECK_SAL", line 2
ORA-04088: 觸發器 'SCOTT.TR_CHECK_SAL' 執行過程當中出錯


--開發示例4
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;

--建立視圖
CREATE OR REPLACE VIEW emp_dept
AS
SELECT d.deptno,d.dname,e.empno,e.ename
FROM dept d, emp e
WHERE d.deptno = e.deptno;


--建立替代觸發器
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;

--測試
INSERT INTO emp_dept VALUES(50,'DEVELOPMENT',2222,'ALICE');

SELECT * FROM EMP_DEPT

--鏈接sys用戶


--建立事件表
CREATE TABLE event_table(
event varchar2(50),
event_time date
);

--再建立一個系統觸發器
create or replace trigger startup_trigger
after startup on database
begin
insert into event_table values(ora_sysevent,SYSDATE);
end;

--在SQLPLUS窗口執行下列命令SHUTDOWNSTARTUPSELECT * FROM event_table;

相關文章
相關標籤/搜索