触发器语句

您还未登录!|登录|注册|帮助
CSDN首页
资讯
论坛
博客
下载
搜索
更多
CTO俱乐部
学生大本营
培训充电
移动开发
软件研发
云计算
程序员
ITeye
TUP
chinayuan的专栏
目录视图
摘要视图
订阅
精创之作《雷神的微软平台安全宝典》诚邀译者                                           移动业界领袖会议·上海·6.20
CSDN博客频道“移动开发之我见”主题征文活动         【分享季1】:网友推荐130个经典资源,分享再赠分!
数据库触发器
分类: 数据库 2011-03-31 18:00 2889人阅读 评论(2) 收藏 举报
触发器
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
  for each row
  when (new_value.department_id<>80 )
begin
  :new_valuemission_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_valuemission_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’,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 *ployees
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)); 42led10
中国证监会微博
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;
2 、 行触发器
是指为受到影响的各个行激活的触发器,定义与语句触发器类似,有以下两个例外:
1 、 定义语句中包含 FOR EACH ROW 子句
2 、 在 BEFORE …… FOR EACH ROW 触发器中,用户可以引用受到影响的行值。
比如:
定义:
create trigger biufer_employees_department_id
  before insert or update
  of department_id
  on employees_copy
  referencing old as old_value
    new as new_value

本文发布于:2024-09-23 02:31:45,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/425197.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:触发器   语句   用户   进行   记录   移动
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议