【Sequence】序列操作start with,nextval,currval三者之间的“复杂”关系与重要结论...

通过这篇文章谈一下在使用Sequence时候需要注意的事项。细节重于一切。

1.创建测试用序列S
sec@ora10g> drop sequence s;
sec@ora10g> create sequence s start with 99;

Sequence created.

2.使用seq简单查看一下s的基本信息
sec@ora10g> select * from seq where SEQUENCE_NAME = 'S';

SEQUENCE_NAME MIN_VALUE  MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
------------- --------- ---------- ------------ - - ---------- -----------
S                     1 1.0000E+27            1 N N         20          99

3.使用dbms_metadata得到序列的详细的创建语句
sec@ora10g> set linesize 150
sec@ora10g> set longchunksize 1000
sec@ora10g> select dbms_metadata.get_ddl('SEQUENCE','S') as "Create Sequence Statements" from dual;

Create Sequence Statements
------------------------------------------------------------------------------------------------------------------------------------------------------

   CREATE SEQUENCE  "SEC"."S"  MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 99 CACHE 20 NOORDER  NOCYCLE

通过上面的创建,我们得到的序列s是从99开始的。

4.在对s取nextval时,推测一下,得到的值是多少??
重要结论一:在初创建的Sequence上第一次使用nextval的时候,得到是初始值,不是初始值加一!
请看下面的演示,得到的是初始化的99值,不是100!!
sec@ora10g> select s.nextval from dual;

   NEXTVAL
----------
        99

如果此时再继续取nextval的话,一切恢复正常
sec@ora10g> select s.nextval from dual;

   NEXTVAL
----------
       100

sec@ora10g> select s.nextval from dual;

   NEXTVAL
----------
       101

5.新开启的session中允许直接使用currval取Sequence的当前值么?
重要结论二:第一次NEXTVAL初始化之后才能使用CURRVAL取值。
sec@ora10g> conn sec/sec
Connected.
sec@ora10g> select s.currval from dual;
select s.currval from dual
       *
ERROR at line 1:
ORA-08002: sequence S.CURRVAL is not yet defined in this session

sec@ora10g> select s.nextval from dual;

   NEXTVAL
----------
       102

sec@ora10g> select s.currval from dual;

   CURRVAL
----------
       102

6.虽然上面看到currval必须在nextval之后使用,不过,可以在一条SQL语句中同时得到nextval和currval值,而且它们在SQL中不分先后顺序,请看下面的演示。
sec@ora10g> conn sec/sec
Connected.
sec@ora10g> select s.currval from dual;
select s.currval from dual
*
ERROR at line 1:
ORA-08002: sequence S.CURRVAL is not yet defined in this session


sec@ora10g> select s.currval, s.nextval from dual;

   CURRVAL    NEXTVAL
---------- ----------
       103        103

sec@ora10g> conn sec/sec
Connected.
sec@ora10g> select s.currval from dual;
select s.currval from dual
*
ERROR at line 1:
ORA-08002: sequence S.CURRVAL is not yet defined in this session


sec@ora10g> select s.nextval, s.currval from dual;

   NEXTVAL    CURRVAL
---------- ----------
       104        104

7.将Oracle 10gR2官方文档关于序列的create和alter的命令语法copy一份在此,便于参考
CREATE SEQUENCE [ schema. ]sequence
   [ { INCREMENT BY | START WITH } integer
   | { MAXVALUE integer | NOMAXVALUE }
   | { MINVALUE integer | NOMINVALUE }
   | { CYCLE | NOCYCLE }
   | { CACHE integer | NOCACHE }
   | { ORDER | NOORDER }
   ]
     [ { INCREMENT BY | START WITH } integer
     | { MAXVALUE integer | NOMAXVALUE }
     | { MINVALUE integer | NOMINVALUE }
     | { CYCLE | NOCYCLE }
     | { CACHE integer | NOCACHE }
     | { ORDER | NOORDER }
     ]... ;


ALTER SEQUENCE [ schema. ]sequence
  { INCREMENT BY integer
  | { MAXVALUE integer | NOMAXVALUE }
  | { MINVALUE integer | NOMINVALUE }
  | { CYCLE | NOCYCLE }
  | { CACHE integer | NOCACHE }
  | { ORDER | NOORDER }
  }
    [ INCREMENT BY integer
    | { MAXVALUE integer | NOMAXVALUE }
    | { MINVALUE integer | NOMINVALUE }
    | { CYCLE | NOCYCLE }
    | { CACHE integer | NOCACHE }
    | { ORDER | NOORDER }
    ]... ;


8.小结
通过这个小实验,得到的结论有:
1)结论一:在初创建的Sequence上第一次使用nextval的时候,得到是初始值,不是初始值加一!
2)结论二:第一次NEXTVAL初始化之后才能使用CURRVAL取值;
3)结论三:可以在一条SQL语句中同时得到nextval和currval值;
4)结论四:从上面的alter sequence的语法看,可以得到这样一个结论,无法使用alter语句修改序列的当前值。

很有必要重点关注以上这些结论,尤其是第一条。

-- The End --

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/519536/viewspace-617172/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/519536/viewspace-617172/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值