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>


分享到:


相關文章: