手机版
你好,游客 登录 注册
背景:
阅读新闻

Oracle 建立序列以及触发器的建立

[日期:2013-12-05] 来源:Linux社区  作者:arrowzz [字体:大 中 小]

序列:序列的创建方法,以及插入数据时的使用;

--序列的创建
create sequence sq
increment by 1
start with 1
maxvalue 10
minvalue 1
cycle
cache 5

--一般(一个序列可以用在多张表,但是一般情况下,一张表对应一个序列)
create sequence sq
increment by 1
start with 1
nocache
nocycle

--使用序列的方法
insert into emp(empno,ename)
values(sq.nextval,'Tim');

--查看数据
select * from emp;

触发器:

--触发器:特殊的存储过程。
--特点:无法直接手动调用,只能自动触发(由一个动作去触发)。
--类型:dml触发器、instead of替代触发器、系统触发器
--dml触发器
--1、语句级(执行操作语句时,只触发一次)
--语法:
create or replace trigger tri_XXX
动作  on  表
declare
  ......
begin
  ......
end;

--例子:给30号部门的员工集体涨工资200元(触发一个语句级触发器)。
--建立触发器(类似于先把存储过程建好,等着被调用)
create or replace trigger tri_update_emp
after update on emp  --修改动作完成后执行触发器
begin
  dbms_output.put_line('涨工资了......');
end;
--触发
update emp set sal=sal+200 where deptno=30;


--建立触发器(类似于先把存储过程建好,等着被调用)
create or replace trigger tri_update_emp
before update on emp  --修改之前触发
begin
  dbms_output.put_line('涨工资了......');
end;
--触发
update emp set sal=sal+200 where deptno=30;

--2、行级(没修改一行,都要触发一次)
create or replace trigger tri_update_emp
after update on emp  --修改动作完成后执行触发器
for each row
begin
  dbms_output.put_line('涨工资了......');
end;
--触发
update emp set sal=sal+200 where deptno=30;

--例子
create or replace trigger tri_up_del_ins_emp
after delete or insert or update on emp  --修改动作完成后执行触发器
for each row
begin
  dbms_output.put_line('触发了......');
end;
--触发
delete from emp where deptno=30;

--条件谓词(布尔类型):inserting 、updating、 deleting
create or replace trigger tri_up_del_ins_emp
after delete or insert or update on emp  --修改动作完成后执行触发器
for each row
begin
  if inserting then
    dbms_output.put_line('又来新人了,oo......');
  elsif updating then
    dbms_output.put_line('修改了,能行不......');
  else
    dbms_output.put_line('被开除了......');
  end if;
 
end;
--触发
delete from emp where deptno=30;

--实例:简易图书管理系统
select * from book;
select * from borrow;
--增加一列
alter table book
add countOfBook integer check(countOfBook>=0);
--完成借书功能,需要borrow表插入一行,book表对应书籍库存-1。
--问题1、触发器建在哪个表上? borrow
--问题2、怎么把插入borrow表的数据传给book? :NEW
--*****行级触发器,自带了两个特殊变量
-- :new --自动存放新插入的数据记录(一行数据),和修改之后的记录行
-- :old --自动存放被删除的数据记录(一行数据),和修改之前的记录行
--例子
create or replace trigger tr_up_emp
after update on emp
for each row
begin
  dbms_output.put_line(:old.ename||:old.sal);
  dbms_output.put_line(:new.ename||:new.sal);
end;
--触发
update emp set ename='ao-smith',sal=250
where ename='SMITH';

SELECT * FROM book;
--实现借书功能
--第一步: 在borrow上建立触发器,用来自动修改book表
create or replace trigger tri_in_borrow
after insert on borrow
for each row
begin
  update book set countOfBook=countOfBook-1
  where bid=:new.bid;
end;
--第二步:只需在borrow中插入数据就OK
insert into borrow
values('T013','1002','B003',sysdate,null);

--作业:P322 11、12, 上面例子中的借书功能 

更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2013-12/93574p2.htm

相关阅读:

Oracle触发器的使用 http://www.linuxidc.com/Linux/2013-03/81396.htm

Oracle触发器给表自身的字段重新赋值出现ORA-04091异常 http://www.linuxidc.com/Linux/2013-07/87075.htm

Oracle创建触发器调用含参数存储过程 http://www.linuxidc.com/Linux/2013-02/80018.htm

Oracle触发器查询统计本表 http://www.linuxidc.com/Linux/2013-01/77671.htm

更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

linux
相关资讯       Oracle触发器  Oracle建立序列 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款