[英]Oracle Sequence starting with 2 instead of 1
Unexpected behavior: 意外行为:
I am encountering strange behavior of Oracle sequences with 11g (works with 10g): 我遇到了11g的Oracle序列的奇怪行为(适用于10g):
CREATE SEQUENCE test_sequence START WITH 1;
CREATE TABLE test_table ( val INT );
INSERT INTO test_table VALUES ( test_sequence.NEXTVAL );
Even though the sequence starts with 1 , the first value inserted is 2 : 即使序列以1开头,插入的第一个值为2 :
SELECT * FROM test_table;
VAL
----------
2
Expected behavior: 预期行为:
Selecting NEXTVAL
without the insert works as expected: 选择不带插入的
NEXTVAL
按预期工作:
CREATE SEQUENCE test_sequence_2 START WITH 1;
SELECT test_sequence_2.NEXTVAL FROM dual
NEXTVAL
----------
1
Question: 题:
Can anyone reproduce this using Oracle 11g? 任何人都可以使用Oracle 11g重现这一点吗? Is this a known issue?
这是一个已知的问题?
I'm using 我正在使用
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
. Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
。
This is documented in the 11.2 SQL Language Reference where it says, 这在11.2 SQL语言参考中有记录 ,它说,
If you attempt to insert a sequence value into a table that uses deferred segment creation, the first value that the sequence returns will be skipped.
如果尝试将序列值插入到使用延迟段创建的表中,则将跳过序列返回的第一个值。
See the link in Jeffrey Kemp's answer for a My Oracle Support (Metalink) note and a workaround. 请参阅Jeffrey Kemp关于My Oracle Support(Metalink)注释和解决方法的答案中的链接。
I'd say the cause is this "undocumented feature". 我要说原因是这个“无证件”。 See My Oracle Support Document ID 1273858.1 (which is unfortunately behind a paywall and cannot be copied here).
请参阅My Oracle Support Document ID 1273858.1(遗憾的是,它位于付费专区后面,无法在此处复制)。
Try it without deferred segment creation and see if the problem persists. 在没有延迟段创建的情况下尝试它,看看问题是否仍然存在。
I can't reproduce on 11G, ie the table contains a 1 after following your steps. 我无法在11G上重现,即在执行完步骤后表格中包含1。
However, it is debatable whether this should be considered an "issue", because sequences are never guaranteed to be gap-free. 然而,这是否应被视为“问题”是有争议的,因为序列永远不会保证无间隙。 What START WITH guarantees is that the sequence will never return a value lower than the specified starting value - eg to avoid conflicts with existing data.
START WITH保证的是序列永远不会返回低于指定起始值的值 - 例如,以避免与现有数据冲突。 I do agree however that what you are seeing is surprising and I would be interested to know the reason!
我同意你所看到的是令人惊讶的,我很想知道原因!
Use: 采用:
CREATE SEQUENCE SQ_SEQUENCE_NAME
INCREMENT BY 1
START WITH 1
MINVALUE 0 -- This will ensure start at 1!
MAXVALUE 99
NOCYCLE
NOCACHE
ORDER;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.