Re: help with serial type - Mailing list pgsql-general

From Gregory Wood
Subject Re: help with serial type
Date
Msg-id 010401c0ce86$6ef0aca0$7889ffcc@comstock.com
Whole thread Raw
In response to help with serial type  (Jason <gee308@mediaone.net>)
List pgsql-general
> Sorry, I got it to work now, I think it had to do with the syntax, so what
> I was trying to do was the right command, I just wrote it out wrong.
Thanks
> for the help.
> I have 1 more question, if I use the INSERT command without telling it
which
> colmuns I want to update, can I still make the 'id' update automatically?
> so I do something like:
>
> INSERT INTO atable VALUES('SQL3', 'whatever command i need to make id
upadte
> automatically',date('now'));
>
> instead of using:
> INSERT INTO atable(column1,column2,column3) VALUES('a','b','c'));

You could explicit use the default of the serial column. i.e.:

INSERT INTO atable VALUES ('SQL3',nextval('atable_id_seq'),date('now'));

Just to clear up the mystery of the SERIAL datatype... it's actually just a
shortcut for the following:

1. Make the serial column an integer type (specifically int4).
2. Add a NOT NULL constraint to that column.
3. Create a sequence called 'tablename_serialfield_seq'.
4. Assign the serial column a default value of
nextval('tablename_serialfield_seq').
5. Create a UNIQUE index on that column.

What this means is that you can:

o Omit the column and have an autoincremented value inserted implicitly
o Add the default value sequence value explicitly
o Use whatever value you want (as long as it's an integer, not NULL, and is
unique to that column).

Greg


pgsql-general by date:

Previous
From: "Anzo"
Date:
Subject: how to migrate from SQL Server7 to PostgreSQL7??
Next
From: "Clayton Vernon"
Date:
Subject: Re: JDBC speed question.