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;
猜你喜欢
- JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它基于ECMAScript的一个子集。 JSON
- vue使用formData传递文件类型的数据1.需求 传递文件类型的数据和其
- 1、模拟退火算法退火是金属从熔融状态缓慢冷却、最终达到能量最低的平衡态的过程。模拟退火算法基于优化问题求解过程与金属退火过程的相似性,以优化
- 我设计第一篇网页的时候,就遇到了字体的设置问题。我发现如果用软件约定的字体大小,则显示效果会很难看的。我是用FrontPage2000作网页
- Pycharm实然提示过期了,网上找了很多Pycharm激活码,要么只支持老版本,要么是不能用。费了半天时间终于在一公众号里找到一枚可以使用
- 注意:本文基于Python2.4完成;如果看到不明白的词汇请记得百度谷歌或维基,whatever。 1. 正则表达式基础 1.1. 简单介绍
- 在开发应用程序时,客户端(前端页面或APP)与服务端交互是在所难免的,在交互过程传递数据时,最通用和流行格式便是JSON,Go语言提供了en
- 在应用系统中,尤其在联机事务处理系统中,对数据查询及处理速度已成为衡量应用系统成败的标准。而采用索引来加快数据处理速度也成为广大数据库用户所
- 例如:性别 gender 男 女 保密基本语法:enum(数据值 1,数据值 2...);数据值列表在 255 个以内,使用 1 个字节来存
- 如下所示:#利用小波分析进行特征分析#参数初始化inputfile= 'C:/Users/Administrator/Desktop
- 在客户端请求的时候增加了自定义的http头,请求如下所示: 自定义http请求头var_dump(getallheaders);一
- 关于CIFAR10数据集的使用主要解决了如何把数据集与transforms结合在一起的问题。CIFAR10的官方解释torchvision.
- 该章节我们来学习一下在 Python 中去创建并使用多进程的方法,通过学习该章节,我们将可以通过创建多个进程来帮助我们提高脚本执行的效率。可
- 介绍在本文中,你将学习如何使用 Python 构建人脸识别系统。人脸识别比人脸检测更进一步。在人脸检测中,我们只检测人脸在图像中的位置,但在
- 1、 HTTP服务器. 2、性能测试工具推荐 3、 图片篇. 4、 如何加载JS,JS应该放在什么位置. 5、 为什么要减少请求数,如何减少
- Logminer是每个Dba都应熟悉的工具,当一天由于用户的误操作你需要做不完全的恢复时,由于你无法确定这个操作是哪个时间做的,所以这对你的
- SQL Server中的cmd_shell组件功能强大,几乎可通过该组建实现Windows系统的所有功能,正因此,这个组件也是SQL Ser
- 本篇介绍Python中的引用。首先想一想如图示例。在python中,值是靠引用来传递来的。用id()来判断两个变量是否为同一个值的引用。如图
- 方法1:/** 功能:数据备份/恢复文件简易方法* 以日期为单位,一天一个备份文件,以当天最后备份为准* 用提交表单的形式进行操作,* 其中
- 1 获取轮廓OpenCV2获取轮廓主要是用cv2.findContoursimport numpy as npimport cv2im =