oracle觸發器

<code>一、觸發器:是一個與表關聯的、存儲的PL/SQL程序,當用戶執行了insert、update、delete操作之後, 

oracle自動地執行觸發器中定義的語句序列。

​\t作用:

\t1.數據確認:如員工漲薪後,新工資不能少於之前的工資。

\t2.安全性檢查:如禁止非工作時間插入新員工。

\t3.做審計,跟蹤上所做的數據操作等。

4.數據的備份與同步。

類型:

語句級觸發器:在指定的操作語句之前或者之後執行一次,不管這個語句影響了多少行語句。

行級觸發器:觸發語句作用的每一條記錄都被觸發,在行級觸發器中使用old和new偽記錄變量,
識別值的狀態/<code>

二、語法

<code>---創建觸發器
create [or replace] trigger 觸發器名
before/after
insert/update/delete [of 列名]
on 表名
[for each row [when(條件)]]
declare
...
begin
\tPLSQL塊

end

---刪除觸發器
drop trigger 觸發器名/<code>

三、實例

<code>---新員工入職後,輸出 "歡迎加入" 字符串。創建觸發器
create or replace trigger trig_show_hello
after ---after 表示操作後觸發
insert on emp
declare
begin
dbms_output.put_line('歡迎加入');
end;

---插入員工。插入成功後就會觸發上面的 trig_show_hello 觸發器
insert into emp
values(9996,'華安','MANAGER','7698',sysdate,9888.87,300,30);

---更新所有員工的薪水,同一加 100,創建觸發器,更新完成後給出提示
create or replace trigger tric_update_sal
after update on emp
for each row --表示行級觸發器
declare
begin
--- :old 表示操作前的記錄行,:new 表操作後的記錄行
dbms_output.put_line('原來工資:'||:old.sal|| ' 現在薪水:'||:new.sal);
end;

---更新員工薪水。自動觸發上面的 tric_update_sal 觸發器
update emp set sal = sal+100;/<code>


分享到:


相關文章: