PostgreSQL自动增量 [英] PostgreSQL Autoincrement

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

问题描述

我正在从MySQL切换到PostgreSQL,想知道如何实现自动增量值.我在PostgreSQL文档中看到一个数据类型为"serial",但是在使用它(在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天全站免登陆