2015-01-09 75 views
2

我想了解nextval如何增加一個序列: 在INSERT INTO ... VALUES語句中, nextval將增加序列的兩倍。但僅限於第一個這樣的陳述。 我不明白在ORACLE manualoracle中的sequence.nextval似乎增加了兩倍

CREATE TABLE test_table(a INT); 
CREATE SEQUENCE seqa START WITH 10; 
CREATE SEQUENCE seqb START WITH 10; 

INSERT INTO test_table VALUES(seqb.NEXTVAL); 
INSERT INTO test_table VALUES(seqb.NEXTVAL); 
SELECT * FROM test_table; 

所得TEST_TABLE對於這種現象的原因:

A 
----- 
11 
12 

而:

SELECT seqa.NEXTVAL FROM DUAL; -- returns 10 
+1

你看到[這個延遲段創建行爲(http://stackoverflow.com/q/4153807/266304)?這是[記錄在這裏](http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_6015.htm#SQLRF01314)? –

+0

哦,是的,我錯過了重複。謝謝! –

+1

對不起,該鏈接已死亡;它[這裏記錄](http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_6015.htm#SQLRF55572)(目前!)。 –

回答

1

這不會發生,我(在Oracle 12C ):

SQL> CREATE TABLE test_table(a INT); 

Table created. 

SQL> CREATE SEQUENCE seqa START WITH 10; 

Sequence created. 

SQL> CREATE SEQUENCE seqb START WITH 10; 

Sequence created. 

SQL> INSERT INTO test_table VALUES(seqb.NEXTVAL); 

1 row created. 

SQL> INSERT INTO test_table VALUES(seqb.NEXTVAL); 

1 row created. 

SQL> SELECT * FROM test_table; 

     A 
---------- 
     10 
     11 
1

注意我沒有意識到這個問題有11g標籤,我的答案是特定於12c。對升級或升級至12c的人可能會有用。

除了@ Tony的回答,在Oracle 12c上,您不需要明確的sequence。利用IDENTITY COLUMNS版本12.1.

例如介紹,

 

    SQL> CREATE TABLE new_identity_table 
     2 (
     3  ID NUMBER GENERATED ALWAYS AS IDENTITY, 
     4  text VARCHAR2(50) 
     5 ); 

    Table created. 

    SQL> 
    SQL> INSERT 
     2 INTO new_identity_table 
     3 (
     4  text 
     5 ) 
     6 VALUES 
     7 (
     8  'This table has an identity column' 
     9 ); 

    1 row created. 

    SQL> column text format A40; 
    SQL> 
    SQL> select * from new_identity_table; 

      ID TEXT 
    ---------- ---------------------------------------- 
      1 This table has an identity column 

    SQL> 

Oracle creates a `sequence` to populate the `identity column`. You can find it named as `ISEQ$$` 

    SQL> select sequence_name, min_value, max_value, increment_by from user_sequences; 

    SEQUENCE_NAME   MIN_VALUE MAX_VALUE      INCREMENT_BY 
    -------------------- ---------- ---------------------------- ------------ 
    ISEQ$$_93199     1 9999999999999999999999999999 1 

    SQL> 

More more information about the identity columns, use the `ALL_TAB_IDENTITY_COLS` view. 

    SQL> SELECT table_name, 
     2   column_name, 
     3   generation_type, 
     4   identity_options 
     5 FROM all_tab_identity_cols 
     6 WHERE owner = 'LALIT' 
     7 ORDER BY 1, 2; 

    TABLE_NAME   COLUMN_NAME  GENERATION IDENTITY_OPTIONS 
    -------------------- --------------- ---------- -------------------------------------------------- 
    NEW_IDENTITY_TABLE ID    ALWAYS  START WITH: 1, INCREMENT BY: 1, MAX_VALUE: 9999999 
                999999999999999999999, MIN_VALUE: 1, CYCLE_FLAG: N 
                , CACHE_SIZE: 20, ORDER_FLAG: N 


    SQL> 
+0

雖然這個問題被標記爲11g。 –

+0

啊,我完全錯過了看到它,被@託尼的答案帶走了。感謝Alex。我會在我的答案中加上一個註釋。 –