`
kb5706
  • 浏览: 40862 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

原文转自:http://space.itpub.net/519536/viewspace-617172



通过这篇文章谈一下在使用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 --



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics