温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ORACLE 12C基础

发布时间:2020-06-29 20:44:15 来源:网络 阅读:2395 作者:whshurk 栏目:关系型数据库

1.CON_ID,0为cdb,1为cdb$root, 2为pdb seed,3以上为pdb
2.自增长列
ORACLE 12C基础
在12c之前,Oracle只能通过sequence来实现这个功能
sys@newtestCDB> create table test(id number generated always as identity , name varchar2(20));

Table created.

Elapsed: 00:00:00.04
sys@newtestCDB> insert into test(name) values('smith');

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(name) values('smith4');

1 row created.

Elapsed: 00:00:00.01
sys@newtestCDB> select * from test;

    ID NAME

     1 smith
     2 smith3
     3 smith4

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 column

Elapsed: 00:00:00.03
sys@newtestCDB> insert into test(id,name) values(null,'smith4');
insert into test(id,name) values(null,'smith4')
*
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,'smith4');
insert into test(id,name) values(2,'smith4')
*
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('smith5');

1 row created.

Elapsed: 00:00:00.01
sys@newtestCDB> select * from test;

    ID NAME

     2 smith3
     4 smith5

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('smith3');

1 row created.

Elapsed: 00:00:00.01
sys@newtestCDB> insert into test(name) values('smith4');

1 row created.

Elapsed: 00:00:00.01
sys@newtestCDB> insert into test(id,name) values(null,'smith4');
insert into test(id,name) values(null,'smith4')
*
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,'smith3');

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,'smith4');

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';

OBJECT_NAME

OBJECT_TYPE

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 表被删除时同时删除
ORACLE 12C基础这是12.1的图,12.2还有新变化

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI