Oracle Sequence starting with 2 instead of 1

前端 未结 4 2010
别那么骄傲
别那么骄傲 2020-12-10 04:16

Unexpected behavior:

I am encountering strange behavior of Oracle sequences with 11g (works with 10g):

CREATE SEQUENCE test_sequence         


        
相关标签:
4条回答
  • 2020-12-10 05:00

    I can't reproduce on 11G, i.e. the table contains a 1 after following your steps.

    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 - e.g. to avoid conflicts with existing data. I do agree however that what you are seeing is surprising and I would be interested to know the reason!

    0 讨论(0)
  • 2020-12-10 05:02

    This is documented in the 11.2 SQL Language Reference where it says,

    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.

    0 讨论(0)
  • 2020-12-10 05:04

    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;
    
    0 讨论(0)
  • 2020-12-10 05:17

    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).

    Try it without deferred segment creation and see if the problem persists.

    0 讨论(0)
提交回复
热议问题