Welcome 微信登录

首页 / 数据库 / MySQL / Oracle 建立序列以及触发器的建立

序列:序列的创建方法,以及插入数据时的使用;--序列的创建
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.htmOracle触发器给表自身的字段重新赋值出现ORA-04091异常 http://www.linuxidc.com/Linux/2013-07/87075.htmOracle创建触发器调用含参数存储过程 http://www.linuxidc.com/Linux/2013-02/80018.htmOracle触发器查询统计本表 http://www.linuxidc.com/Linux/2013-01/77671.htm更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12
  • 1
  • 2
  • 下一页
remote_listener 设置问题导致数据库不能打开Oracle PLSQL 打包相关资讯      Oracle触发器  Oracle建立序列 
  • Oracle数据库中的触发器  (03/11/2015 10:12:29)
  • Oracle利用触发器实现自增列  (02/10/2015 11:27:09)
  • Oracle中的System Triggers(DDL触  (02/25/2014 19:33:55)
  • Oracle中的触发器  (02/14/2015 11:22:03)
  • Oracle触发器问题解决一例  (11/11/2014 17:36:10)
  • Oracle 触发器更新基表不同记录所  (12/31/2013 10:01:29)
本文评论 查看全部评论 (0)
表情: 姓名: 字数