這三個對象都可以實現自增,這里從如下幾個維度來看看這幾個對象有哪些不同,其中功能性上看,大部分特性都是一致的或者類似的。
1、sequence在所有數據庫中的性質都一樣,它是跟具體的字段不是強綁定的,其特點是支持多個對個對象之間共享。
sequence作為自增字段值的時候,對表的寫入需要另外單獨授權sequence(GRANT USAGE ON SEQUENCE test_old_id_seq;)
sequence類型的字段表,在使用CREATE TABLE new_table LIKE old_table的時候,新表的自增字段會已久指向原始表的sequence
結論:
對于自增字段,無特殊需求的情況下,sequence不適合作為“自增列”,作為最最次選。
2、identity本質是為了兼容標準sql中的語法而新加的,修復了一些serial的缺陷,比如無法通過alter table的方式實現增加或者刪除serial字段
2.1 identity定義成generated by default as identity也允許顯式插入,
2.2 identity定義成always as identity,加上overriding system value也可以顯式不插入
結論:
identity是serial的“增強版”,更適合作為“自增列”使用。
3、sequence,serial,identity共同的缺點是在顯式插入之后,無法將自增值更新為表中的最大Id,這一點再顯式插入的情況下是潛在自增字段Id沖突的
結論:
自增列在顯式插入之后,一定要手動重置為表的最大Id。
4、自增字段的update沒有細看,相對來說自增列的顯式插入是一種常規操作,那些對自增列的update操作,只要腦子沒問題,一般是不會這么干的。
原始手稿,懶得整理了,不涉及原理性的東西,動手試一遍就明白了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
---------------------------------------------------------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' ); --插入失敗,主鍵重復,因為序列自身是遞增的,不會關心表中被顯式插入的數據 insert into myschema.test_seq ( name ) values ( 'fff' ); --重置序列的最大值 select setval( 'myschema.seq_1' ,( select max (id) from myschema.test_seq):: BIGINT ); --事務回滾后,序列號并不會回滾 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; --顯式插入,可以執行 insert into myschema.test_serial(id, name ) values (5, 'ccc' ); --再次隱式插入,第二次會報錯,因為隱式插入的話,serial會基于顯式插入之前的Id做自增,serial無法意識到當前已經存在的最大值 insert into myschema.test_serial( name ) values ( 'xxx' ); insert into myschema.test_serial( name ) values ( 'yyy' ); select * from myschema.test_serial; --truncate table 后serial不會重置 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; --驗證是否會隨著事務一起回滾,結論:不會 begin ; insert into myschema.test_serial( name ) values ( 'yyy' ); rollback ; --重置serial,需要注意的是重置的Id必須要大于相關表的字段最大Id,否則會產生重號 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' ); --顯式插入后,繼續隱式插入,第二次插入會報錯,identity已久是不識別表中顯式插入后的最大值 insert into myschema.test_identiy_2( name ) values ( 'xxx' ); insert into myschema.test_identiy_2( name ) values ( 'yyy' ); select * from myschema.test_identiy_2; 總之個identity很扯淡,你定義成always as identity,加上overriding system value可以顯式不插入 定義成generated by default as identity也允許顯式插入 不管怎么樣,既然都允許顯式插入,那扯什么淡的來個overriding system value --truncate后再次插入,自增列不會重置 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后,會重置自增列 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; |
實際中更改identity自增長列的當前起始值(已有的最大值+1):
補充:PostgreSQL不同的表使用不同的自增序列
hibernate 配置文件里面應該是這樣的:
1
2
3
4
5
|
<id name = "id" > <generator class= "sequence" > <param name = "sequence" >adminuser</param> </generator> </id> |
使用xdoclet時 類里面的配置應該是這樣的:
1
2
3
4
5
6
|
/** * @hibernate.id generator-class= "sequence" * @hibernate.generator-param name = "sequence" value= "adminuser" */ private int id; |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://www.cnblogs.com/wy123/p/13367486.html