1.CON_ID,0爲cdb,1爲cdb$root, 2爲pdb seed,3以上爲pdb
2.自增加列
在12c以前,Oracle只能經過sequence來實現這個功能
sys@newtestCDB> create table test(id number generated always as identity , name varchar2(20));ide
Table created.code
Elapsed: 00:00:00.04
sys@newtestCDB> insert into test(name) values('smith');orm
1 row created.blog
Elapsed: 00:00:00.01
sys@newtestCDB> insert into test(name) values('smith2');dns
1 row created.get
Elapsed: 00:00:00.01
sys@newtestCDB> insert into test(name) values('smith3');it
1 row created.table
Elapsed: 00:00:00.01
sys@newtestCDB> select * from test;form
ID NAME
1 smith 2 smith2 3 smith3
Elapsed: 00:00:00.02
sys@newtestCDB> update test set id=1 where id=2;
update test set id=1 where id=2
*
ERROR at line 1:
ORA-32796: cannot update a generated always identity columnclass
Elapsed: 00:00:00.03
sys@newtestCDB> insert into test(id,name) values(null,'smith3');
insert into test(id,name) values(null,'smith3')
*
ERROR at line 1:
ORA-32795: cannot insert into a generated always identity column
Elapsed: 00:00:00.01
sys@newtestCDB> insert into test(id,name) values(2,'smith3');
insert into test(id,name) values(2,'smith3')
*
ERROR at line 1:
ORA-32795: cannot insert into a generated always identity column
sys@newtestCDB> delete from test where id=3;
1 row deleted.
Elapsed: 00:00:00.03
sys@newtestCDB> insert into test(name) values('smith4');
1 row created.
Elapsed: 00:00:00.01
sys@newtestCDB> select * from test;
ID NAME
2 smith2 4 smith4
Elapsed: 00:00:00.01
結論:
GENERATED ALWAYS AS IDENTITY 能夠不指定該列進行插入
GENERATED ALWAYS AS IDENTITY不能在該列中插入NULL值
GENERATED ALWAYS AS IDENTITY不能指定具體值插入
GENERATED ALWAYS AS IDENTITY 不能使用update更新該列
sys@newtestCDB> create table test(id number generated by default as identity , name varchar2(20));
Table created.
Elapsed: 00:00:00.03
sys@newtestCDB> insert into test(name) values('smith');
1 row created.
Elapsed: 00:00:00.01
sys@newtestCDB> insert into test(name) values('smith2');
1 row created.
Elapsed: 00:00:00.01
sys@newtestCDB> insert into test(name) values('smith3');
1 row created.
Elapsed: 00:00:00.01
sys@newtestCDB> insert into test(id,name) values(null,'smith3');
insert into test(id,name) values(null,'smith3')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYS"."TEST"."ID")
Elapsed: 00:00:00.02
sys@newtestCDB> insert into test(id,name) values(2,'smith2');
1 row created.
Elapsed: 00:00:00.01
sys@newtestCDB> update test set id = NULL where id=2;
update test set id = NULL where id=2
*
ERROR at line 1:
ORA-01407: cannot update ("SYS"."TEST"."ID") to NULL
結論:
GENERATED BY DEFAULT AS IDENTITY 能夠不指定該列進行插入
GENERATED BY DEFAULT AS IDENTITY不能在該列中插入NULL值
GENERATED BY DEFAULT AS IDENTITY 能夠指定具體值插入
GENERATED BY DEFAULT AS IDENTITY 能夠使用update更新該列,但不能更新爲NULL
sys@newtestCDB> create table test(id number generated by default ON NULL as identity , name varchar2(20));
Table created.
Elapsed: 00:00:00.03
sys@newtestCDB> insert into test(id,name) values(null,'smith3');
1 row created.
Elapsed: 00:00:00.01
結論:
GENERATED BY DEFAULT ON NULL AS IDENTITY 能夠不指定該列進行插入
GENERATED BY DEFAULT ON NULL AS IDENTITY 方式能夠指定具體值插入
GENERATED BY DEFAULT ON NULL AS IDENTITY 能夠在該列中插入null值
GENERATED BY DEFAULT ON NULL AS IDENTITY 能夠使用update更新該列
sys@newtestCDB> col tablename format A20
sys@newtestCDB> col table_name format A20
sys@newtestCDB> col sequence_name format A20
sys@newtestCDB> SELECT a.name AS table_name,
2 b.name AS sequence_name
3 FROM sys.idnseq$ c
4 JOIN obj$ a ON c.obj# = a.obj#
5 JOIN obj$ b ON c.seqobj# = b.obj#
6 where a.name='TEST';
TABLE_NAME SEQUENCE_NAME
TEST ISEQ$$_83962
Elapsed: 00:00:00.01
sys@newtestCDB> create table test(id number generated by default as identity , name varchar2(20));
Table created.
Elapsed: 00:00:00.03
sys@newtestCDB> SELECT a.name AS table_name,
2 b.name AS sequence_name
3 FROM sys.idnseq$ c
4 JOIN obj$ a ON c.obj# = a.obj#
5 JOIN obj$ b ON c.seqobj# = b.obj#
6 where a.name='TEST';
TABLE_NAME SEQUENCE_NAME
TEST ISEQ$$_83964
Elapsed: 00:00:00.01
sys@newtestCDB> SELECT object_name, object_type FROM user_objects where object_name='ISEQ$$_83964';
ISEQ$$_83964
SEQUENCE
Elapsed: 00:00:00.05
sys@newtestCDB> drop table test;
Table dropped.
Elapsed: 00:00:00.04
sys@newtestCDB> SELECT object_name, object_type FROM user_objects where object_name='ISEQ$$_83964';
no rows selected
Elapsed: 00:00:00.00
sys@newtestCDB> create table test(id number generated by default as identity , name varchar2(20));
Table created.
Elapsed: 00:00:00.06
sys@newtestCDB> SELECT a.name AS table_name,
2 b.name AS sequence_name
3 FROM sys.idnseq$ c
4 JOIN obj$ a ON c.obj# = a.obj#
5 JOIN obj$ b ON c.seqobj# = b.obj#
6 where a.name='TEST';
TABLE_NAME SEQUENCE_NAME
TEST ISEQ$$_83966
Elapsed: 00:00:00.01
sys@newtestCDB> drop SEQUENCE ISEQ$$_83966;
drop SEQUENCE ISEQ$$_83966
*
ERROR at line 1:
ORA-32794: cannot drop a system-generated sequence
Elapsed: 00:00:00.02
結論:
Identity Columns 是基於序列實現的
GENERATED IDENTITY 中sequence不能單獨被刪除
GENERATED IDENTITY 中sequence 表被刪除時同時刪除這是12.1的圖,12.2還有新變化