首页 / ORACLE / oracle中触发器的讲解
oracle中触发器的讲解
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了oracle中触发器的讲解,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2727字,纯文字阅读大概需要4分钟。
内容图文
![oracle中触发器的讲解](/upload/InfoBanner/zyjiaocheng/457/307c5f2ee17743baa953d2f28cc3ae22.jpg)
触发器在数据库里以独立的对象存储,它与存储过程和函数不同的是,存储过程与函数需要用户显示调用才执行,而触发器是由一个事件来启动运行。即触发器是当某个事件发生时自动地隐式运行。并且,触发器不能接收参数。所以运行触发器就叫触发或点火(firing)。ORACLE事件指的是对数据库的表进行的INSERT、UPDATE及DELETE操作或对视图进行类似的操作。ORACLE将触发器的功能扩展到了触发ORACLE,如数据库的启动与关闭等。所以触发器常用来完成由数据库的完整性约束难以完成的复杂业务规则的约束,或用来监视对数据库的各种操作,实现审计的功能。
create sequence seq_userInfo_usid start with 1001;
create or replace triggle tri_userInfo_usid --创建或替换触发器tri_userInfo_usid
before insert on userInfo --在向userInfo表中添加 记录之前的触发
for each row --没影响一行出发一次
begin --触发之后执行下面的语句
select seq_userInfo_usid.nextval into :new.usid from dual; --:new新值 :old老值
end;
案列
--日志 触发器讲解
--查看所有触发器
select * from user_triggers;
--禁用触发器
alert trigger tri_log_uname(触发器名字) disable;
--激活触发器
alert trigger tri_log_uname(触发器名字) enble;
--重新编译
alert trigger tri_log_uname complie;
--禁用某个表上的触发器
alert table <table_name> diable all triggers;
create table log(
uname varchar2(20),
action varchar2(10),
acttime date
);
--从U001开始,以递增的形式对uname进行命名
create sequence seq_log_uname start with 1001;
create or replace trigger tri_log_uname
before insert on log
for each row
begin
select ‘U‘||substr(seq_log_uname.nextval,1) into :new.uname from dual;
end;
insert into log values(1,‘yc‘,sysdate);
select * from log;
create or replace trigger tri_log
after insert or update or delete on dept
for each row --for each 行级触发,执行语句没影响一行出发一次,默认是语句级触发,没执行一条语句触发一次,无论该语句影响多少行,
begin
if inserting then
insert into log values(user,‘insert‘,sysdate);
elsif updating then
insert into log values(user,‘update‘,sysdate);
elsif deleting then
insert into log values(user,‘delete‘,sysdate);
end if;
end;
select * from dept;
insert into dept values(80,‘技术部‘,‘湖南衡阳‘);
select * from log;
--更改dept和emp中的deptno的值(涉及主外键)
create or replace trigger tri_dept
before update on dept
for each row
begin
update emp set deptno=:new.deptno where deptno=:old.deptno;
end;
update dept set deptno=88 where deptno=20;
select * from dual;
select * from dept;
select * from emp;
create or replace trigger tri_emp
before update of sal on emp --当修改emp表中的sal列在值时
for each row
when(new.sal<=old.sal)
begin
select 3000 into :new.sal from dual;
end;
select * from emp;
update emp set sal=4000 where empno=7369;
oracle中触发器的讲解
标签:
本文系统来源:http://www.cnblogs.com/yaobolove/p/4781971.html
内容总结
以上是互联网集市为您收集整理的oracle中触发器的讲解全部内容,希望文章能够帮你解决oracle中触发器的讲解所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。