注意我沒有意識到這個問題有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>
你看到[這個延遲段創建行爲(http://stackoverflow.com/q/4153807/266304)?這是[記錄在這裏](http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_6015.htm#SQLRF01314)? –
哦,是的,我錯過了重複。謝謝! –
對不起,該鏈接已死亡;它[這裏記錄](http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_6015.htm#SQLRF55572)(目前!)。 –