PostgreSQL 自增 [英] PostgreSQL Autoincrement

查看:39
本文介绍了PostgreSQL 自增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 MySQL 切换到 PostgreSQL,并且想知道如何进行自动增量值.我在 PostgreSQL 文档中看到数据类型为串行",但在使用它时出现语法错误(在 v8.0 中).

I'm switching from MySQL to PostgreSQL and was wondering how I can do autoincrement values. I saw in the PostgreSQL docs a datatype "serial", but I get syntax errors when using it (in v8.0).

推荐答案

是的,SERIAL 是等效的功能.

Yes, SERIAL is the equivalent 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 只是一个围绕序列的创建表时间宏.您不能将 SERIAL 更改为现有列.

SERIAL is just a create table time macro around sequences. You can not alter SERIAL onto an existing column.

这篇关于PostgreSQL 自增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆