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
2009-04-24 22:16:57
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
2010-02-01 20:54:20
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
2010-02-01 21:07:34
quoting the table name is a really bad practice
Evan Carroll
2010-02-02 04:51:26
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
2010-02-05 16:03:23