oracle 触发器 学习笔记

来源:asp之家 时间:2009-05-24 19:57:00 

功能:
1、 允许/限制对表的修改
2、 自动生成派生列,比如自增字段
3、 强制数据一致性
4、 提供审计和日志记录
5、 防止无效的事务处理
6、 启用复杂的业务逻辑
开始
create trigger biufer_employees_department_id
before insert or update
of department_id
on employees
referencing old as old_value
new as new_value
for each row
when (new_value.department_id<>80 )
begin
:new_value.commission_pct :=0;
end;
/
触发器的组成部分:
1、 触发器名称
2、 触发语句
3、 触发器限制
4、 触发操作
1、 触发器名称
create trigger biufer_employees_department_id
命名习惯:
biufer(before insert update for each row)
employees 表名
department_id 列名
2、 触发语句
比如:
表或视图上的DML语句
DDL语句
数据库关闭或启动,startup shutdown 等等
before insert or update
of department_id
on employees
referencing old as old_value
new as new_value
for each row
说明:
1、 无论是否规定了department_id ,对employees表进行insert的时候
2、 对employees表的department_id列进行update的时候
3、 触发器限制
when (new_value.department_id<>80 )
限制不是必须的。此例表示如果列department_id不等于80的时候,触发器就会执行。
其中的new_value是代表跟新之后的值。
4、 触发操作
是触发器的主体
begin
:new_value.commission_pct :=0;
end;
主体很简单,就是将更新后的commission_pct列置为0
触发:
insert into employees(employee_id,
last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )
values( 12345,'Chen','Donny', sysdate, 12, ‘donny@hotmail.com',60,10000,.25);
select commission_pct from employees where employee_id=12345;
触发器不会通知用户,便改变了用户的输入值。
触发器类型:
1、 语句触发器
2、 行触发器
3、 INSTEAD OF 触发器
4、 系统条件触发器
5、 用户事件触发器
1、 语句触发器
是在表上或者某些情况下的视图上执行的特定语句或者语句组上的触发器。能够与INSERT、UPDATE、
DELETE或者组合上进行关联。但是无论使用什么样的组合,各个语句触发器都只会针对指定语句激活一次
。比如,无论update多少行,也只会调用一次update语句触发器。
例子:
需要对在表上进行DML操作的用户进行安全检查,看是否具有合适的特权。
Create table foo(a number);
Create trigger biud_foo
Before insert or update or delete
On foo
Begin
If user not in (‘DONNY') then
Raise_application_error(-20001, ‘You don't have access to modify this table.');
End if;
End;
/
即使SYS,SYSTEM用户也不能修改foo表
[试验]
对修改表的时间、人物进行日志记录。
1、 建立试验表
create table employees_copy as select *from hr.employees
2、 建立日志表
create table employees_log(
who varchar2(30),
when date);
3、 在employees_copy表上建立语句触发器,在触发器中填充employees_log 表。
Create or replace trigger biud_employee_copy
Before insert or update or delete
On employees_copy
Begin
Insert into employees_log(
Who,when)
Values( user, sysdate);
End;
/
4、 测试
update employees_copy set salary= salary*1.1;
select *from employess_log;
5、 确定是哪个语句起作用?
即是INSERT/UPDATE/DELETE中的哪一个触发了触发器?
可以在触发器中使用INSERTING / UPDATING / DELETING 条件谓词,作判断:
begin
if inserting then
-----
elsif updating then
-----
elsif deleting then
------
end if;
end;
if updating(‘COL1') or updating(‘COL2') then
------
end if;
[试验]
1、 修改日志表
alter table employees_log
add (action varchar2(20));
2、 修改触发器,以便记录语句类型。
Create or replace trigger biud_employee_copy
Before insert or update or delete
On employees_copy
Declare
L_action employees_log.action%type;
Begin
if inserting then
l_action:='Insert';
elsif updating then
l_action:='Update';
elsif deleting then
l_action:='Delete';
else
raise_application_error(-20001,'You should never ever get this error.');
Insert into employees_log(
Who,action,when)
Values( user, l_action,sysdate);
End;
/
3、 测试
insert into employees_copy( employee_id, last_name, email, hire_date, job_id)
values(12345,'Chen','Donny@hotmail',sysdate,12);
select *from employees_log
update employees_copy set salary=50000 where employee_id = 12345;

标签:oracle,触发器,笔记
0
投稿

猜你喜欢

  • 防采集,几种觉得有用的防采集方法

    2009-09-03 13:30:00
  • Python PIL库图片灰化处理

    2022-08-18 20:57:30
  • vant中的toast轻提示实现代码

    2024-04-26 17:38:53
  • JavaScript 判断浏览器类型及版本

    2024-05-13 10:36:39
  • react native环境安装流程

    2023-07-02 10:22:36
  • python中pandas.DataFrame的简单操作方法(创建、索引、增添与删除)

    2021-05-15 16:25:58
  • Python无法用requests获取网页源码的解决方法

    2023-04-24 07:38:04
  • pyinstaller打包后,配置文件无法正常读取的解决

    2022-12-17 18:22:09
  • 利用Python提取图片经纬度并锁定拍照地点

    2021-10-18 01:10:40
  • mysql5.7.19 winx64安装配置方法图文教程(win10)

    2024-01-20 20:12:45
  • Tornado高并发处理方法实例代码

    2022-10-13 15:30:07
  • 对Golang import 导入包语法详解

    2024-02-20 19:10:28
  • Python在字典中获取带权重的随机值实现方式

    2022-12-11 05:21:44
  • 配置 SQL Server 2005 以允许远程连接的方法

    2024-01-13 12:58:40
  • insert select与select into 的用法使用说明

    2012-01-05 18:47:58
  • 浅谈tensorflow 中tf.concat()的使用

    2023-07-21 20:24:08
  • 对dataframe进行列相加,行相加的实例

    2023-07-24 16:57:59
  • python基于paramiko库远程执行 SSH 命令,实现 sftp 下载文件

    2022-11-09 23:31:31
  • python爬虫 正则表达式使用技巧及爬取个人博客的实例讲解

    2023-11-06 02:16:54
  • JavaScript判断undefined类型的正确方法

    2024-04-10 11:04:05
  • asp之家 网络编程 m.aspxhome.com