我试图了解nextval如何递增序列: 在下面看来,在一个INSERT INTO ... VALUES声明中, nextval将增加两倍的序列。但仅限于第一个这样的陈述。 我在ORACLE manual
中不理解这种行为的原因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
答案 0 :(得分: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 :(得分: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>