oracle创建sequence

 创建:

create sequence seq_test 
	minvalue 1 
	maxvalue 21 
	start with 1 
	increment by 1 
	cache 20 
	cycle 
	order;

minvalue 1 / nominvalue:最小值为1
maxvalue 21 / nomaxvalue:最大值为21
start with 1:从1开始计数
increment by 1:每次增加1
cache 20 / nocache:

        缓存20个sequence值 /  不缓存

        如果缓存,则会有跳号的危险:如果指定cache值,oracle就可以预先在内存里面放置一些sequence,这样存取的快些。内存里面的取完后,oracle自动再取一组到内存。数据库突然不正常down掉,内存中的sequence就会丢失

cycle / nocycle:

        cycle,即如果到达最大值21后,再次从1开始

SQL> select seq_test.nextval from dual;
 
   NEXTVAL
----------
        21
 
SQL> select seq_test.nextval from dual;
 
   NEXTVAL
----------
         1

        nocycle:

SQL> select seq_test.nextval from dual;
 
   NEXTVAL
----------
        21
 
SQL> select seq_test.nextval from dual;
 
select seq_test.nextval from dual
 
ORA-08004: sequence SEQ_TEST.NEXTVAL exceeds MAXVALUE and cannot be instantiated

order  / noorder:

        order:guarantees sequence numbers are generated in order of request,按照请求的顺序生成sequence值。如果sequence值为时间戳比较重要,作为主键order选项没什么用

        noorder:does not guarantee sequence numbers are generated in order of
request.

 

查询:

currval=返回sequence的当前值
nextval=增加sequence的值,然后返回sequence 值
在使用currval之前必须先使用nextval:
SQL> select seq_test.currval from dual;
 
select seq_test.currval from dual
 
ORA-08002: sequence SEQ_TEST.CURRVAL is not yet defined in this session
 
SQL> select seq_test.nextval from dual;
 
   NEXTVAL
----------
         1
 
SQL> select seq_test.currval from dual;
 
   CURRVAL
----------
         1

第一次使用nextval返回的是初始值,以后再使用nextvl返回的是currval+'increment by'的值:

SQL> select seq_test.nextval from dual;
 
   NEXTVAL
----------
         1
  
SQL> select seq_test.nextval from dual;
 
   NEXTVAL
----------
         2

 

删除:

SQL> drop sequence seq_test;
 
Sequence dropped

 


 

posted @ 2013-05-24 17:29  心意合一  阅读(550)  评论(0编辑  收藏  举报