williambirkin

恭喜发财!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

合了2好的文章
http://oracle.chinaitlab.com/exploiture/528353.html
http://blog.csdn.net/kj110/archive/2006/05/18/744423.aspx

1.
基本
1 建序列命
CREATE SEQUENCE [user.]sequence_name
    [INCREMENT BY n]
    [
START WITH n]
    [maxvalue n | nomaxvalue]
    [minvalue n | nominvalue]
    [CYCLE|NOCYCLE]
    [CACHE|NOCACHE]
    [ORDER|NOORDER]
    ;
    INCREMENT BY
指定序列号之隔,该值正的或的整数,但不可0序列升序。忽略子句,缺省值为1
    START WITH
:指定生成的第一个序列号。在升序,序列可从比最小大的值开始,缺省值为序列的最小于降序,序列可由比最大小的值开始,缺省值为序列的最大
   
MAXVALUE:指定序列可生成的最大
    NOMAXVALUE
升序指定最大值为1027降序指定最大值为-1
    MINVALUE
:指定序列的最小
    NOMINVALUE
升序指定最小值为1降序指定最小值为-1026
    CYCLE
:循使用,用大最大再返。 CACHE:指定cache。如果指定CACHEoracle就可以先在内存里面放置一些sequence这样存取的快些。cache里面的取完后,oracle再取一cache 使用cache会跳号, 比如数据突然不正常down掉(shutdown abort),cache中的sequence就会. 所以可以在create sequence候用nocache防止这种情况。
    ORDER
序使用序列

2 更改序列
    ALTERSEQUENCE [user.]sequence_name
    [INCREMENT BY n]
    [MAXVALUE n| NOMAXVALUE ]
    [MINVALUE n | NOMINVALUE]
    [CYCLE|NOCYCLE]
    [CACHE|NOCACHE]
    [ORDER|NOORDER]
   

3 除序列
    DROP SEQUENCE [user.]sequence_name

2. 序列的使用
序列提供两个方法,NextValCurrVal
NextVal
:取序列的下一个,一次NEXTVAL会增加一次sequence
CurrVal
:取序列的当前

但是要注意的是:第一次NEXTVAL返回的是初始;随后的NEXTVAL会自增加你定INCREMENT BY,然后返回增加后的CURRVAL是返回当前SEQUENCE,但是在第一次NEXTVAL初始化之后才能使用CURRVAL,否会出


3.
连续编号表 SEQUENCE + TRIGGER
3.1
建表
create or replace table
test_table
(
 id
d  number(3),
 
named  varchar2(20)
);
/

3.2 建序列sequence_test_table_id
create sequence "
sequence_test_table_id" increment by 1 start with 1 minvalue 1 maxvalue 999 cycle nocache order;

3.3 建触
要使用sequence,用上oracletriger(触器)
create or replace trigger trigger
_insert_sequence
before insert on
test_table
referencing old as old new as new

*表明在向test_table插入一行之前触
for each row  
begin
  *
sequence_test_table_id的下一个值赋给test_table的新idd
  select
sequence_test_table_id.nextval into :new.idd from dual; 

  exception
    when others then
      -- consider logging the error and then re-raise
      raise;
end trigger
_insert_sequence

3.4 测试
insert into test_
table(named) values(sysdate);

如果,我希望序列包含更多的信息,比如日期等,以便日后做统计用,可以
当然可以!

可以在triger那里打主意:)
比如我要在序列里加上日期20030225xxx,后面的xxx表示是从序列里的来
先要更新表:
alter table test_
table modify (id number(11) )   *展列
然后修改triger
create or replace trigger trigger
_insert_sequence
before insert on test_
table
referencing old as old new as new
for each row   begin

  select to_char(sysdate,'yyyymmdd')||lpad(sequence_test_table_id.nextval,3,'0') into :new.idd from dual; 
  

  exception
    when others then
      -- consider logging the error and then re-raise
      raise;
end
trigger_insert_sequence

posted on 2006-11-22 14:11  williambirkin  阅读(3063)  评论(3编辑  收藏  举报