# 而这个序列也是真实存在的,说明是用户自动创建的 xb@PDB12C> select * from user_sequences;
SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER S E S K -------------------- ---------- ---------- ------------ - - ---------- ----------- - - - - ISEQ$$_90071 1 1.0000E+28 1 N N 20 21 N N N N
drop table t2 purge; create table t2 (id number generated always as identity, name varchar2(10));
xb@PDB12C> insert into t2 values(1,'xb'); insert into t2 values(1,'xb') * ERROR at line 1: ORA-32795: cannot insert into a generated always identity column
xb@PDB12C> insert into t2 values(null,'xb'); insert into t2 values(null,'xb') * ERROR at line 1: ORA-32795: cannot insert into a generated always identity column
insert into t2(name) values('xb'); insert into t2(name) values('xb2'); insert into t2(name) values('xb3'); commit;
xb@PDB12C> select * from t2;
ID NAME ---------- ---------- 1 xb 2 xb2 3 xb3
xb@PDB12C> update t2 set id=4 where name='xb'; update t2 set id=4 where name='xb' * ERROR at line 1: ORA-32796: cannot update a generated always identity column
由于是使用的序列,那么也可以用到一些序列中的特性,可以指定不同的初始值和累加值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
drop table t3 purge; create table t3 (id number generated always as identity (start with 2 increment by 2), name varchar2(10));
insert into t3(name) values('xb'); insert into t3(name) values('xb2'); insert into t3(name) values('xb3'); commit;
xb@PDB12C> select * from t3;
ID NAME ---------- ---------- 2 xb 4 xb2 6 xb3
通过USER_TAB_IDENTITY_COLS等视图可以查询到一些标识列的信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
set linesize 300 col table_name for a10 col COLUMN_NAME for a10 col IDENTITY_OPTIONS for a70 select * from USER_TAB_IDENTITY_COLS;
TABLE_NAME COLUMN_NAM GENERATION SEQUENCE_NAME IDENTITY_OPTIONS ---------- ---------- ---------- -------------------- ---------------------------------------------------------------------- T3 ID ALWAYS ISEQ$$_90075 START WITH: 2, INCREMENT BY: 2, MAX_VALUE: 999999999999999999999999999 9, MIN_VALUE: 1, CYCLE_FLAG: N, CACHE_SIZE: 20, ORDER_FLAG: N
T1 ID BY DEFAULT ISEQ$$_90079 START WITH: 1, INCREMENT BY: 1, MAX_VALUE: 999999999999999999999999999 9, MIN_VALUE: 1, CYCLE_FLAG: N, CACHE_SIZE: 20, ORDER_FLAG: N
T2 ID ALWAYS ISEQ$$_90073 START WITH: 1, INCREMENT BY: 1, MAX_VALUE: 999999999999999999999999999 9, MIN_VALUE: 1, CYCLE_FLAG: N, CACHE_SIZE: 20, ORDER_FLAG: N