我正在从 MySQL 切换到 PostgreSQL,并且想知道如何执行自动增量值。我在 PostgreSQL 文档中看到了一个数据类型“serial”,但在使用它时出现语法错误(在 v8.0 中)。
11 回答
是的,SERIAL 是等效的功能。
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 只是一个围绕序列的创建表时间宏。您不能将 SERIAL 更改为现有列。
如果要将序列添加到已经存在的表中的 id 中,可以使用:
CREATE SEQUENCE user_id_seq;
ALTER TABLE user ALTER user_id SET DEFAULT NEXTVAL('user_id_seq');
从 Postgres 10 开始,还支持 SQL 标准定义的标识列:
create table foo
(
id integer generated always as identity
);
除非明确要求,否则创建一个不能被覆盖的标识列。以下插入将失败,列定义为generated always
:
insert into foo (id)
values (1);
然而,这可以被否决:
insert into foo (id) overriding system value
values (1);
使用该选项时,这与现有实现generated by default
的行为基本相同:serial
create table foo
(
id integer generated by default as identity
);
当手动提供值时,也需要手动调整基础序列 - 与serial
列相同。
默认情况下,标识列不是主键(就像serial
列一样)。如果应该是一个,则需要手动定义主键约束。
虽然看起来序列等同于 MySQL auto_increment,但有一些细微但重要的区别:
1.失败的查询增加序列/序列
serial 列在查询失败时递增。这会导致失败查询产生碎片,而不仅仅是行删除。例如,在 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。
如果您要手动创建自己的序列,这仍然会发生:
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;
如果您想测试 MySQL 的不同之处,请在 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);
你应该得到以下没有碎片:
+-----+-------+
| 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 |
+-----+-------+
测试在 MySQL 5.6.33、Linux (x86_64) 和 PostgreSQL 9.4.9 上执行
抱歉,重新讨论一个老问题,但这是 Google 上出现的第一个 Stack Overflow 问题/答案。
这篇文章(首先出现在 Google 上)讨论了使用更新的 PostgreSQL 10 语法: https ://blog.2ndquadrant.com/postgresql-10-identity-columns/
这恰好是:
CREATE TABLE test_new (
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
);
希望有帮助:)
您必须注意不要直接插入到您的 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 的评论,创建SERIAL
隐式创建序列,因此对于上面的示例 -
CREATE TABLE foo (id SERIAL,bar varchar);
CREATE TABLE
会隐式地foo_id_seq
为 serial column创建序列foo.id
。因此,SERIAL
[4 Bytes] 有利于它的易用性,除非您的 id 需要特定的数据类型。
从 PostgreSQL 10 开始
CREATE TABLE test_new (
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
payload text
);
这种方式肯定会奏效,我希望它会有所帮助:
CREATE TABLE fruits(
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL
);
INSERT INTO fruits(id,name) VALUES(DEFAULT,'apple');
or
INSERT INTO fruits VALUES(DEFAULT,'apple');
您可以在下一个链接中查看详细信息: http ://www.postgresqltutorial.com/postgresql-serial/
创建序列。
CREATE SEQUENCE user_role_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 3
CACHE 1;
ALTER TABLE user_role_id_seq
OWNER TO postgres;
并更改表
ALTER TABLE user_roles ALTER COLUMN user_role_id SET DEFAULT nextval('user_role_id_seq'::regclass);