KingbaseES中3種自增長類型sequence,serial,identity的區別:
對象 |
顯示插入 |
顯示插入后更新最大值 |
清空表后是否重置 |
是否跟事務一起回滾 |
多對象共享 |
支持重置 |
出現錯誤后序列值是否增長 |
sequence |
是 |
否 |
否 |
否 |
是 |
是 |
是 |
serial |
是 |
否 |
否 |
否 |
否 |
是 |
是 |
identity |
是 |
否 |
否 |
否 |
否 |
是 |
是 |
1、Sequence
create sequence seq_1 INCREMENT BY 1 MINVALUE 1 START WITH 1; create table test_seq ( id int not null default nextval('seq_1') primary key, name varchar(10) ); 隱式插入 insert into test_seq (name) values ('aaa'); insert into test_seq (name) values ('bbb'); insert into test_seq (name) values ('ccc');
顯式插入 insert into test_seq (id,name) values (5,'ddd');
再次隱式插入 test1=# insert into test_seq (name) values ('eee'); INSERT 0 1 test1=# insert into test_seq (name) values ('fff'); 錯誤: 重復鍵違反唯一約束"test_seq_pkey" 描述: 鍵值"(id)=(5)" 已經存在
再次執行語句可正常插入,序列因為之前的錯誤調用自動增加 test1=# insert into test_seq (name) values ('fff'); INSERT 0 1
重置序列
--重置序列的最大值 select setval('seq_1',(select max(id) from test_seq)::BIGINT); --事務回滾后,序列號並不會回滾 test1=# begin; BEGIN test1=# insert into test_seq (name) values ('ggg'); INSERT 0 1 test1=# rollback; ROLLBACK -- truncate 表之后,序列不受影響 test1=# truncate table test_seq; TRUNCATE TABLE test1=# insert into test_seq (name) values ('ggg'); INSERT 0 1 test1=# select * from test_seq; id | name ----+------ 9 | ggg (1 行記錄) --重置序列 ALTER SEQUENCE seq_1 RESTART WITH 1; test1=# ALTER SEQUENCE seq_1 RESTART WITH 1; ALTER SEQUENCE test1=# insert into test_seq (name) values ('ggg'); INSERT 0 1 test1=# select * from test_seq; id | name ----+------ 9 | ggg 1 | ggg
2、Serial
create table test_serial ( id serial primary key, name varchar(100) ); 隱式插入 insert into test_serial(name) values ('aaa'); insert into test_serial(name) values ('bbb'); insert into test_serial(name) values ('ccc'); 顯示插入 insert into test_serial(id,name) values (5,'ddd); select * from test_serial; --再次隱式插入,第二次會報錯 test1=# insert into test_serial(id,name) values (5,'ddd); INSERT 0 1 test1=# insert into test_serial(name) values ('eee'); INSERT 0 1 test1=# insert into test_serial(name) values ('fff'); 錯誤: 重復鍵違反唯一約束"test_serial_pkey" 描述: 鍵值"(id)=(5)" 已經存在 --再次執行語句可正常插入,序列因為之前的錯誤調用自動增加 test1=# insert into test_serial(name) values ('fff'); INSERT 0 1 --重置serial SELECT SETVAL((SELECT sys_get_serial_sequence(' test_serial', 'id')), 1, false);
3、Identity
Identity是R6版本新增的語法,R3數據庫不支持該語法。
identity定義成generated by default as identity允許顯式插入,
identity定義成always as identity 不允許顯示插入,但是加上overriding system value也可以顯式插入。
create table test_identiy_1 ( id int generated always as identity (START WITH 1 INCREMENT BY 1) primary key , name varchar(100) ); insert into test_identiy_1(name) values ('aaa'); insert into test_identiy_1(name) values ('bbb'); insert into test_identiy_1(name) values ('ccc'); --顯式插入值 如果定義為generated always as identity則不允許顯式插入,除非增加overriding system value 提示。 test1=# insert into test_identiy_1(id,name) values (5,'ccc'); 錯誤: 無法插入到列"id" 描述: 列"id"是定義為GENERATED ALWAYS的標識列. 提示: 使用OVERRIDING SYSTEM VALUE覆蓋. test1=# insert into test_identiy_1(id,name)overriding system value values (5,'ccc'); INSERT 0 1
generate by default:
create table test_identiy_2 ( id int generated by default as identity (START WITH 1 INCREMENT BY 1) primary key , name varchar(100) ); insert into test_identiy_2(name) values ('aaa'); insert into test_identiy_2(name) values ('bbb'); insert into test_identiy_2(name) values ('ccc'); test1=# insert into test_identiy_2(id,name) values (5,'ccc'); INSERT 0 1
重置 Identity
重置Identity的方式有2種: 1. ALTER TABLE test_identiy_1 ALTER COLUMN id RESTART WITH 100; 2. TRUNCATE table test_identiy_1 RESTART IDENTITY;