域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過
這三個(gè)對(duì)象都可以實(shí)現(xiàn)自增,這里從如下幾個(gè)維度來看看這幾個(gè)對(duì)象有哪些不同,其中功能性上看,大部分特性都是一致的或者類似的。
1、sequence在所有數(shù)據(jù)庫中的性質(zhì)都一樣,它是跟具體的字段不是強(qiáng)綁定的,其特點(diǎn)是支持多個(gè)對(duì)個(gè)對(duì)象之間共享。
sequence作為自增字段值的時(shí)候,對(duì)表的寫入需要另外單獨(dú)授權(quán)sequence(GRANT USAGE ON SEQUENCE test_old_id_seq;)
sequence類型的字段表,在使用CREATE TABLE new_table LIKE old_table的時(shí)候,新表的自增字段會(huì)已久指向原始表的sequence
結(jié)論:
對(duì)于自增字段,無特殊需求的情況下,sequence不適合作為“自增列”,作為最最次選。
2、identity本質(zhì)是為了兼容標(biāo)準(zhǔn)sql中的語法而新加的,修復(fù)了一些serial的缺陷,比如無法通過alter table的方式實(shí)現(xiàn)增加或者刪除serial字段
2.1 identity定義成generated by default as identity也允許顯式插入,
2.2 identity定義成always as identity,加上overriding system value也可以顯式不插入
結(jié)論:
identity是serial的“增強(qiáng)版”,更適合作為“自增列”使用。
3、sequence,serial,identity共同的缺點(diǎn)是在顯式插入之后,無法將自增值更新為表中的最大Id,這一點(diǎn)再顯式插入的情況下是潛在自增字段Id沖突的
結(jié)論:
自增列在顯式插入之后,一定要手動(dòng)重置為表的最大Id。
4、自增字段的update沒有細(xì)看,相對(duì)來說自增列的顯式插入是一種常規(guī)操作,那些對(duì)自增列的update操作,只要腦子沒問題,一般是不會(huì)這么干的。
原始手稿,懶得整理了,不涉及原理性的東西,動(dòng)手試一遍就明白了。
---------------------------------------------------------sequence-------------------------------------------------------------
create sequence myschema.seq_1 INCREMENT BY 1 MINVALUE 1 START WITH 1;
create table myschema.test_seq
(
id int not null default nextval('myschema.seq_1') primary key,
name varchar(10)
);
隱式插入
insert into myschema.test_seq (name) values ('aaa');
insert into myschema.test_seq (name) values ('bbb');
insert into myschema.test_seq (name) values ('ccc');
select * from myschema.test_seq;
顯式插入
insert into myschema.test_seq (id,name) values (5,'ddd');
select * from test_seq;
再次隱式插入
--可以正常插入
insert into myschema.test_seq (name) values ('eee');
--插入失敗,主鍵重復(fù),因?yàn)樾蛄凶陨硎沁f增的,不會(huì)關(guān)心表中被顯式插入的數(shù)據(jù)
insert into myschema.test_seq (name) values ('fff');
--重置序列的最大值
select setval('myschema.seq_1',(select max(id) from myschema.test_seq)::BIGINT);
--事務(wù)回滾后,序列號(hào)并不會(huì)回滾
begin;
insert into myschema.test_seq (name) values ('ggg');
rollback;
-- truncate 表之后,序列不受影響
truncate table myschema.test_seq;
--重置序列
ALTER SEQUENCE myschema.seq_1 RESTART WITH 1;
---------------------------------------------------------serial-------------------------------------------------------------
create table myschema.test_serial
(
id serial primary key,
name varchar(100)
)
select * from test_serial;
insert into myschema.test_serial(name) values ('aaa');
insert into myschema.test_serial(name) values ('bbb');
insert into myschema.test_serial(name) values ('ccc');
select * from myschema.test_serial;
--顯式插入,可以執(zhí)行
insert into myschema.test_serial(id,name) values (5,'ccc');
--再次隱式插入,第二次會(huì)報(bào)錯(cuò),因?yàn)殡[式插入的話,serial會(huì)基于顯式插入之前的Id做自增,serial無法意識(shí)到當(dāng)前已經(jīng)存在的最大值
insert into myschema.test_serial(name) values ('xxx');
insert into myschema.test_serial(name) values ('yyy');
select * from myschema.test_serial;
--truncate table 后serial不會(huì)重置
truncate table myschema.test_serial;
insert into myschema.test_serial(name) values ('aaa');
insert into myschema.test_serial(name) values ('bbb');
insert into myschema.test_serial(name) values ('ccc');
select * from myschema.test_serial;
--驗(yàn)證是否會(huì)隨著事務(wù)一起回滾,結(jié)論:不會(huì)
begin;
insert into myschema.test_serial(name) values ('yyy');
rollback;
--重置serial,需要注意的是重置的Id必須要大于相關(guān)表的字段最大Id,否則會(huì)產(chǎn)生重號(hào)
SELECT SETVAL((SELECT pg_get_serial_sequence('myschema.test_serial', 'id')), 1, false);
---------------------------------------------------------identity-------------------------------------------------------------
drop table myschema.test_identiy_1
create table myschema.test_identiy_1
(
id int generated always as identity (cache 100 START WITH 1 INCREMENT BY 1) primary key ,
name varchar(100)
);
create table myschema.test_identiy_2
(
id int generated by default as identity (cache 100 START WITH 1 INCREMENT BY 1) primary key ,
name varchar(100)
);
insert into myschema.test_identiy_1(name) values ('aaa');
insert into myschema.test_identiy_1(name) values ('bbb');
insert into myschema.test_identiy_1(name) values ('ccc');
insert into myschema.test_identiy_2(name) values ('aaa');
insert into myschema.test_identiy_2(name) values ('bbb');
insert into myschema.test_identiy_2(name) values ('ccc');
select * from myschema.test_identiy_1;
--顯式插入值,如果定義為generated always as identity則不允許顯式插入,除非增加overriding system value 提示
--一旦提示了overriding system value,可以
insert into myschema.test_identiy_1(id,name) values (5,'ccc');
insert into myschema.test_identiy_1(id,name)overriding system value values (5,'ccc');
select * from myschema.test_identiy_2;
--顯式插入值,如果定義為generated by default as identity則允許顯式插入,
insert into myschema.test_identiy_2(id,name) values (5,'ccc');
--顯式插入后,繼續(xù)隱式插入,第二次插入會(huì)報(bào)錯(cuò),identity已久是不識(shí)別表中顯式插入后的最大值
insert into myschema.test_identiy_2(name) values ('xxx');
insert into myschema.test_identiy_2(name) values ('yyy');
select * from myschema.test_identiy_2;
總之個(gè)identity很扯淡,你定義成always as identity,加上overriding system value可以顯式不插入
定義成generated by default as identity也允許顯式插入
不管怎么樣,既然都允許顯式插入,那扯什么淡的來個(gè)overriding system value
--truncate后再次插入,自增列不會(huì)重置
truncate table myschema.test_identiy_1;
select * from myschema.test_identiy_1;
begin;
insert into myschema.test_identiy_1(name) values ('xxx');
rollback;
--truncate并且RESTART IDENTITY后,會(huì)重置自增列
TRUNCATE table myschema.test_identiy_1 RESTART IDENTITY;
select * from myschema.test_identiy_1
--identity自增列的重置表或者更改
ALTER TABLE myschema.test_identiy_1 ALTER COLUMN id RESTART WITH 100;
實(shí)際中更改identity自增長列的當(dāng)前起始值(已有的最大值+1):
補(bǔ)充:PostgreSQL不同的表使用不同的自增序列
hibernate 配置文件里面應(yīng)該是這樣的:
<id name="id">
<generator class="sequence">
<param name="sequence">adminuser</param>
</generator>
</id>
使用xdoclet時(shí) 類里面的配置應(yīng)該是這樣的:
/**
* @hibernate.id generator-class="sequence"
* @hibernate.generator-param name="sequence" value="adminuser"
*/
private int id;
文章來源:腳本之家
來源地址:https://www.jb51.net/article/205253.htm
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!