原文转自: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 --
分享到:
相关推荐
START WITH 1 —— 从1开始计数 MAXVALUE —— 最大值值 NOMAXVALUE —— 不设置最大值 NOCYCLE —— 一直累加,不循环 CYCLE CACHE 10; 只要定义了emp_sequence,你就可以用使CURRVAL,NEXTVAL CURRVAL=...
Sequence to Sequence Learning with Neural Networksv论文PDF版
自然语言处理方向机器翻译的经典论文之一。
以上代码完成了一个序列(sequence)的建立过程,名称为emp_sequence,范围是从1开始到无限大(无限大的程度是由你机器决定的),nocycle 是决定不循环,如果你设置了最大值那么你可以用cycle 会使seq到最大之后循环....
Supervised Sequence Labelling with Recurrent Neural Networks,Supervised Sequence Labelling with Recurrent Neural Networks,Supervised Sequence Labelling with Recurrent Neural Networks,
Edward Grefenstette - Beyond Sequence to Sequence with Augmented RNNs
全局子序列比对 Global Sequence Alignment
生成5,6,7阶m序列,并选择最优序列对生成Gold序列
在ORACLE数据库中,序列(SEQUENCE)是使用非常频繁的一个数据库对象,但是有时候会遇到序列(SEQUECNE)跳号(skip sequence numbers)的情形,那么在哪些情形下会遇到跳号呢? 事务回滚引起的跳号 不管序列有...
Matlab产生m序列的函数-m_sequence1.m 一个产生m序列的函数,希望大家喜欢 function [mseqmatrix]= m_sequence1; n = length; N = 2^n-1; register = [zeros 1];%定义移位寄存器的初始状态 mseqmatrix= ...
Empower Sequence Labeling with Task-Aware Neural Language Model
序列(Sequence)是用来生成连续的整数数据的对象。序列常常用来作为主键中增长列, 序列中的可以升序生成,也可以降序生成。创建序列的语法是: 语法结构:创建序列 CREATE SEQUENCE sequence_name [START WITH ...
start with 1 increment by 1 nomaxvalue cache 10; --使用序列 insert into user_table(user_id,user_name,user_pwd) vlaues(seq_user.nextval,'tcy','tcy'); --修改序列 alert sequence seq_user ...
在Oracle数据库移植过程中,sequence可能失效,本资源可使失效的sequence重新恢复作用
TUD的行人检测数据库,tud-crossing-sequence图像序列。
2.霍尔顿(Halton)序列,matlab产生拟随机数
设计m序列发生器,其特征方程为 ,输出数字序列信号m_sequence码速率为10Mbps
对网上现有的产生m序列的线性反馈移位寄存器代码进行了修改,可以通过输入本原多项式向量,生成相应m序列