Re: help with serial type

From: "Gregory Wood" <gregw(at)com-stock(dot)com>
To: "Jason" <gee308(at)mediaone(dot)net>
Cc: "PostgreSQL-General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: help with serial type
Date: 2001-04-26 19:19:39
Message-ID: 010401c0ce86$6ef0aca0$7889ffcc@comstock.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message V. M. 2001-04-26 19:24:02 Re: Re: unanswered: Schema Issue
Previous Message Joao Pedro M. F. Monoo 2001-04-26 19:18:49 Re: random rows