我从MySQL切换到PostgreSQL,我想知道我如何能有一个INT列与自动递增。我在PostgreSQL文档中看到了一个名为SERIAL的数据类型,但我在使用它时遇到了语法错误。
当前回答
这种方法肯定有效,我希望它能有所帮助:
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/
其他回答
虽然看起来序列与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。
如果你手动创建你自己的序列,这仍然会发生:
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. 手动设置Serial Column值会导致后续查询失败。
@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" DETAIL: Key (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上进行
是的,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。
创建序列。
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
ALTER TABLE user_roles ALTER COLUMN user_role_id SET DEFAULT nextval('user_role_id_seq'::regclass);
抱歉,要重复一个老问题,但这是谷歌上弹出的第一个Stack Overflow问题/答案。
这篇文章(首先在谷歌上发布)讨论了在PostgreSQL 10中使用更新后的语法: https://blog.2ndquadrant.com/postgresql-10-identity-columns/
这恰好是:
CREATE TABLE test_new (
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
);
希望这对你有所帮助。
你必须小心不要直接插入到你的SERIAL或sequence字段中,否则当序列达到插入值时,你的写入将失败:
-- 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;
推荐文章
- PostgreSQL删除所有内容
- 为什么PostgreSQL要对索引列进行顺序扫描?
- PostgreSQL INSERT ON冲突更新(upsert)使用所有排除的值
- 如何检查一个表是否存在于给定的模式中
- 如何将整数转换为字符串作为PostgreSQL查询的一部分?
- Psycopg2:用一个查询插入多行
- PostgreSQL返回JSON数组的结果集?
- PostgreSQL通配符LIKE用于单词列表中的任何一个
- 检查Postgres JSON数组是否包含字符串
- psql: FATAL: Peer authentication failed for user "dev"
- 如何在Postgres/SQL中获得两个整数的最小/最大值?
- 如何在postgresql中显示函数,过程,触发器源代码?
- 如何用postgres将间隔转换为小时数?
- 在postgresql中将查询结果按月和年分组
- 如何改变一个列的数据类型在PostgreSQL表?