views:

8176

answers:

1

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).

+16  A: 

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 is just a create table time macro around sequences. You can not alter SERIAL onto an existing column.

Trey
Since which version could this datatype be found? I've been using sequences.nextval in pgsql 'cause I haven't noticed such a thing. Thanks.
Alfabravo
as he said, it is just a macro around sequences. the SERIAL type is just an integer, and a sequence, with the column default to the sequences next value. Unlike MySQL there is nothing special and voodooey that goes on.
Evan Carroll
quoting the table name is a really bad practice
Evan Carroll
Quoting the table names is a habit since I inherited a DB that had mixed case names and quoting table names is a requirement of use.
Trey