PLSQL触发器详解-2

上传人:M****1 文档编号:499979888 上传时间:2023-01-21 格式:DOC 页数:10 大小:49KB
返回 下载 相关 举报
PLSQL触发器详解-2_第1页
第1页 / 共10页
PLSQL触发器详解-2_第2页
第2页 / 共10页
PLSQL触发器详解-2_第3页
第3页 / 共10页
PLSQL触发器详解-2_第4页
第4页 / 共10页
PLSQL触发器详解-2_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《PLSQL触发器详解-2》由会员分享,可在线阅读,更多相关《PLSQL触发器详解-2(10页珍藏版)》请在金锄头文库上搜索。

1、Oracle触发器详细介绍Oracle触发器详细介绍一触发器是特定事件出现的时候,自动执行的代码块。类似于存储过程,但是用户不能直接调用他们。功能: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 fo

2、r each row when (new_value.department_id80 )begin :new_mission_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 等等befo

3、re 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_id80 )限制不是必须的。此例表示如果列department_id不等于80的时候,触发器就会执行。其中的new_value是代表更

4、新之后的值。4、 触发操作是触发器的主体begin :new_mission_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, ,60,10000,.25);select commission_pct from employees where emp

5、loyee_id=12345;触发器不会通知用户,便改变了用户的输入值。触发器类型:1、 语句触发器2、 行触发器3、 INSTEAD OF 触发器4、 系统条件触发器5、 用户事件触发器注释:before和after:指在事件发生之前或之后激活触发器。instead of:如果使用此子句,表示可以执行触发器代码来代替导致触发器调用的事件。insert、delete和update:指定构成触发器事件的数据操纵类型,update还可以制定列的列表。referencing:指定新行(即将更新)和旧行(更新前)的其他名称,默认为new和old。table_or_view_name:指要创建触发器的表

6、或视图的名称。for each row:指定是否对受影响的每行都执行触发器,即行级触发器,如果不使用此子句,则为语句级触发器。when:限制执行触发器的条件,该条件可以包括新旧数据值得检查。declare-end:是一个标准的PL/SQL块。 Oracle触发器详细介绍二-语句触发器1、 语句触发器是在表上或者某些情况下的视图上执行的特定语句或者语句组上的触发器。能够与INSERT、UPDATE、DELETE或者组合上进行关联。但是无论使用什么样的组合,各个语句触发器都只会针对指定语句激活一次。比如,无论update多少行,也只会调用一次update语句触发器。例子:需要对在表上进行DML操作

7、的用户进行安全检查,看是否具有合适的特权。Create table foo(a number);Create trigger biud_foo Before insert or update or delete On fooBegin If user not in (DONNY) then Raise_application_error(-20001, You dont have access to modify this table.); End if;End;/即使SYS,SYSTEM用户也不能修改foo表试验对修改表的时间、人物进行日志记录。1、 建立试验表create table em

8、ployees_copy as select * from hr.employees2、 建立日志表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) Va

9、lues( 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

10、 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 upd

11、ating 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,

12、 job_id) values(12345,Chen,Donnyhotmail,sysdate,12);select *from employees_log总结:语句级触发器.(语句级触发器对每个DML语句执行一次)是在表上或者某些情况下的视图上执行的特定语句或者语句组上的触发器。能够与INSERT、UPDATE、DELETE或者组合上进行关联。但是无论使用什么样的组合,各个语句触发器都只会针对指定语句激活一次。比如,无论update多少行,也只会调用一次update语句触发器。实例:create or replace trigger tri_testafter insert or update or delete on testbeginif updating thendbms_output.put_line(修改);elsif deleting thendbms_output.put_line(删除);elsif inserting thendbms_output.put_line(插入);end if;end; Oracle触发器详细介绍三-行级触发器行级触发器本章介绍行级触发器机制。大部分

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 小学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号