PostgreSQL自动增量

我正在从MySQL切换到PostgreSQL,并想知道如何做自动增量值。 我在PostgreSQL文档中看到了一个数据types“serial”,但是在使用它的时候出现了语法错误(在v8.0中)。

是的,SERIAL是等效function。

CREATE TABLE foo ( id SERIAL, bar varchar); INSERT INTO foo (bar) values ('blah'); INSERT INTO foo (bar) values ('blah'); SELECT * FROM foo; 1,blah 2,blah 

SERIAL仅仅是一个创build表格时间macros的序列。 您不能将SERIAL更改为现有的列。

您可以使用任何其他整数数据types ,例如smallint

例如:

 CREATE SEQUENCE user_id_seq; CREATE TABLE user ( user_id smallint NOT NULL DEFAULT nextval('user_id_seq') ); ALTER SEQUENCE user_id_seq OWNED BY user.user_id; 

最好使用自己的数据types,而不是用户的串行数据types 。

如果你想添加序列到已经存在的表中的id,你可以使用:

 CREATE SEQUENCE user_id_seq; ALTER TABLE user ALTER user_id SET DEFAULT NEXTVAL('user_id_seq'); 

虽然它看起来像序列是相当于 MySQL auto_increment,但有一些微妙但重要的区别:

1.失败的查询递增顺序/串行

串行列在失败的查询中增加。 这导致失败的查询碎片化,而不仅仅是行删除。 例如,在PostgreSQL数据库上运行以下查询:

 CREATE TABLE table1 ( uid serial NOT NULL PRIMARY KEY, col_b integer NOT NULL, CHECK (col_b>=0) ); INSERT INTO table1 (col_b) VALUES(1); INSERT INTO table1 (col_b) VALUES(-1); INSERT INTO table1 (col_b) VALUES(2); SELECT * FROM table1; 

你应该得到以下输出:

  uid | col_b -----+------- 1 | 1 3 | 2 (2 rows) 

请注意,uid从1到3而不是1到2。

这仍然会发生,如果您要手动创build自己的序列:

 CREATE SEQUENCE table1_seq; CREATE TABLE table1 ( col_a smallint NOT NULL DEFAULT nextval('table1_seq'), col_b integer NOT NULL, CHECK (col_b>=0) ); ALTER SEQUENCE table1_seq OWNED BY table1.col_a; 

如果您想testingMySQL的不同之处,请在MySQL数据库上运行以下命令:

 CREATE TABLE table1 ( uid int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, col_b int unsigned NOT NULL ); INSERT INTO table1 (col_b) VALUES(1); INSERT INTO table1 (col_b) VALUES(-1); INSERT INTO table1 (col_b) VALUES(2); 

你应该得到以下没有fragementation

 +-----+-------+ | uid | col_b | +-----+-------+ | 1 | 1 | | 2 | 2 | +-----+-------+ 2 rows in set (0.00 sec) 

2.手动设置串行列值会导致将来的查询失败。

@trev在之前的回答中指出了这一点。

为了模拟这个,手动将uid设置为4,稍后“冲突”。

 INSERT INTO table1 (uid, col_b) VALUES(5, 5); 

表格数据:

  uid | col_b -----+------- 1 | 1 3 | 2 5 | 5 (3 rows) 

运行另一个插入:

 INSERT INTO table1 (col_b) VALUES(6); 

表格数据:

  uid | col_b -----+------- 1 | 1 3 | 2 5 | 5 4 | 6 

现在如果你运行另一个插入:

 INSERT INTO table1 (col_b) VALUES(7); 

它将失败,并显示以下错误消息:

错误:重复键值违反唯一约束“table1_pkey”细节:键(uid)=(5)已经存在。

相比之下,MySQL将如下所示优雅地处理:

 INSERT INTO table1 (uid, col_b) VALUES(4, 4); 

现在插入另一行而不设置uid

 INSERT INTO table1 (col_b) VALUES(3); 

查询不会失败,uid只是跳转到5:

 +-----+-------+ | uid | col_b | +-----+-------+ | 1 | 1 | | 2 | 2 | | 4 | 4 | | 5 | 3 | +-----+-------+ 

testing是在MySQL 5.6.33上进行的,适用于Linux(x86_64)和PostgreSQL 9.4.9

您必须小心,不要直接插入SERIAL或序列字段,否则当序列达到插入值时,写入将失败:

 -- Table: "test" -- DROP TABLE test; CREATE TABLE test ( "ID" SERIAL, "Rank" integer NOT NULL, "GermanHeadword" "text" [] NOT NULL, "PartOfSpeech" "text" NOT NULL, "ExampleSentence" "text" NOT NULL, "EnglishGloss" "text"[] NOT NULL, CONSTRAINT "PKey" PRIMARY KEY ("ID", "Rank") ) WITH ( OIDS=FALSE ); -- ALTER TABLE test OWNER TO postgres; INSERT INTO test("Rank", "GermanHeadword", "PartOfSpeech", "ExampleSentence", "EnglishGloss") VALUES (1, '{"der", "die", "das", "den", "dem", "des"}', 'art', 'Der Mann küsst die Frau und das Kind schaut zu', '{"the", "of the" }'); INSERT INTO test("ID", "Rank", "GermanHeadword", "PartOfSpeech", "ExampleSentence", "EnglishGloss") VALUES (2, 1, '{"der", "die", "das"}', 'pron', 'Das ist mein Fahrrad', '{"that", "those"}'); INSERT INTO test("Rank", "GermanHeadword", "PartOfSpeech", "ExampleSentence", "EnglishGloss") VALUES (1, '{"der", "die", "das"}', 'pron', 'Die Frau, die nebenen wohnt, heißt Renate', '{"that", "who"}'); SELECT * from test; 

在被问及的问题和回答@ sereja1c的评论中,创buildSERIAL隐含地创build序列,因此对于上面的例子 –

 CREATE TABLE foo (id SERIAL,bar varchar); 

CREATE TABLE将为串行列foo.id隐式创build序列foo.id 。 因此,除非你需要一个特定的数据types,否则SERIAL [4 Bytes]对于它的易用性是有好处的。

从Postgres 10开始,还支持由SQL标准定义的标识列:

 create table foo ( id integer generated always as identity ); 

除非明确要求,否则创build一个不能被覆盖的标识列。 以下插入操作将失败,并将列定义为generated always

 insert into foo (id) values (1); 

这可以被否决:

 insert into foo (id) overriding system value values (1); 

当使用generated by default的选项时generated by default这与现有的serial实现基本上是相同的行为:

 create table foo ( id integer generated by default as identity ); 

如果手动提供值,则需要手动调整基础序列 – 与serial列相同。


标识列默认情况下不是主键(就像serial列一样)。 如果它应该是一个,主键约束需要手动定义。