oracle 12C identity新特性

IDENTITY Columns is a new feature introduced in Oracle Release 12c to support the American National Standards Institute (ANSI) SQL keyword IDENTITY.

Identity Columns are automatically incrementing columns. They simplify application development and migration from other RDBMS to Oracle. This feature make use of a default sequence generator and hence the columns will be assigned an increasing or decreasing integer value from this sequence generator.

1 這種情況,insert不能給id1字段賦值,會自動賦值,例如values (1,'name1')會失敗

SQL> create table tab_sql_idnty4 (
id1 number generated by default as identity ,
name varchar2(10)
);
Table created.



SQL> insert into tab_sql_idnty4(name) values ('name1');
1 row created.
commit

SQL> select * from tab_sql_idnty4;

ID1 NAME


1 name1

For the Identity Columns, we can either have the default sequence to generate the values or only to provide the value when a NULL is inserted. Based on this the definition of an identity column may specify GENERATED ALWAYS OR GENERATED BY DEFAULT keywords.

Generated By Default: Oracle uses the sequence generator to assign a value to the column by default. But you can also explicitly assign a specified value to the column. If you specify "ON Null" then sequence generator assigns a value to the column if you insert NULL value into the column.

SQL> create table tab_sql_idnty4 (
id1 number generated by default as identity ,
name varchar2(10)
);
Table created.



SQL> insert into tab_sql_idnty4(name) values ('name1');
1 row created.
commit

SQL> insert into tab_sql_idnty4 values (NULL,'name1');
insert into tab_sql_idnty4 values (NULL,'name1')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("XXX"."TAB_SQL_IDNTY4"."ID1")



SQL> select * from tab_sql_idnty4;

ID1 NAME


1 name1

2 想給identity字段賦值可以通過下面的方式:

While inserting NULL

create table tab_sql_idnty (
id1 number generated by default on NULL as identity ,
name varchar2(10));

SQL> desc tab_sql_idnty
Name Null? Type


ID1 NOT NULL NUMBER
NAME VARCHAR2(10)

insert into tab_sql_idnty values (1, 'name1');
insert into tab_sql_idnty values(20,'name2');
insert into tab_sql_idnty values (NULL,'name3');

SQL> select * from tab_sql_idnty;

ID1 NAME


1 name1
20 name2
1 name3

3 以下模式中identity字段不能賦值,也不能修改

Generated Always: Column cannot be updated by the users , so the default value given by sequence generator is the only possible value for the column. An error is raised if a user tries to insert into or update the column.

create table tab_sql_idnty2 (
id1 number generated always as identity ,
name varchar2(10));

SQL> desc tab_sql_idnty2
Name Null? Type


ID1 NOT NULL NUMBER
NAME VARCHAR2(10)

SQL> insert into tab_sql_idnty2 values (1,'name1');
insert into tab_sql_idnty2 values (1,'name1')
*
ERROR at line 1:
ORA-32795: cannot insert into a generated always identity column



insert into tab_sql_idnty2 values (NULL,'name1');
SQL> insert into tab_sql_idnty2 values (NULL,'name1');
insert into tab_sql_idnty2 values (NULL,'name1')
*
ERROR at line 1:
ORA-32795: cannot insert into a generated always identity column




SQL> insert into tab_sql_idnty2(name) values ('name1');
1 row created.
SQL> insert into tab_sql_idnty2(name) values ('name2');
1 row created.
SQL> insert into tab_sql_idnty2(name) values ('name3');
1 row created.
SQL> commit;
Commit complete.






SQL> select * from tab_sql_idnty2;
ID1 NAME


1 name1
2 name2
3 name3

SQL> update tab_sql_idnty2 set id1=4 where id1=2;
update tab_sql_idnty2 set id1=4 where id1=2
*
ERROR at line 1:
ORA-32796: cannot update a generated always identity column



Identity column definition can be specified in "Create Table" or "Alter Table" statement. We can configure the sequence generator using the identity_options_clause.

create table tab_sql_idnty3 (
id1 number generated always as identity (start with 6 increment by 2),
name varchar2(10)
);


SQL> insert into tab_sql_idnty3(name) values ('name1');
SQL> insert into tab_sql_idnty3(name) values ('name2');
SQL> insert into tab_sql_idnty3(name) values ('name3');
SQL> commit;


SQL> select * from tab_sql_idnty3;

ID1 NAME


6 name1
8 name2
10 name3
You can query the columns IDENTITY_COLUMN,DEFAULT_ON_NULL,DATA_DEFAULT etc from user_tab_columns to query about identity columns.


SQL> select TABLE_NAME,COLUMN_NAME,DEFAULT_ON_NULL,IDENTITY_COLUMN,DATA_DEFAULT from user_tab_columns;

TABLE_NAME COLUMN_NAME DEFAULT_ON_NULL IDENTITY_COLUMN DATA_DEFAULT


TAB_SQL_IDNTY ID1 YES YES "XXX"."ISEQ$$_94457".nextval
TAB_SQL_IDNTY NAME NO NO
TAB_SQL_IDNTY3 ID1 NO YES "XXX"."ISEQ$$_94453".nextval
TAB_SQL_IDNTY3 NAME NO NO
TAB_SQL_IDNTY4 ID1 NO YES "XXX"."ISEQ$$_94455".nextval
TAB_SQL_IDNTY4 NAME NO NO
These are the restrictions for Identity Colum feature in 12c:





i) Only one identity column per table.
ii) Identity column data type should be numeric. Cannot be a user-defined data type.
iii) The column definition clause cannot have DEFAULT clause for identity columns.
iv) With identity clause, not null and non deferrable constraints are implicitly specified.
v) Create table as select will not inherit the identity property on a column.



identity屬性刪除:ALTER TABLE Test_alter MODIFY id DROP IDENTITY;
identity列刪除: ALTER TABLE Test_alter DROP id;(通過ctas重建表也可以去掉identity屬性)
identity其實值修改:alter table your_table modify (id generated by default on null as identity
start with limit value);
在已存在表上添加一個identity屬性的字段:ALTER Table Test_alter
(ADD new_id INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH 1
INCREMENT BY 2
MAXVALUE 100
CACHE 10
CYCLE));









修改identity屬性:
CREATE Table Test_alter (
id INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH 1
INCREMENT BY 2
MAXVALUE 100
CACHE 10
CYCLE),
name STRING, PRIMARY KEY (id)
);








ALTER TABLE Test_alter (MODIFY id GENERATED BY DEFAULT AS IDENTITY
(START WITH 1000
INCREMENT BY 3
MAXVALUE 5000
CACHE 1
CYCLE)
);





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章