From: Tom Lane <tgl@sss.pgh.pa.us>
To: Seth Banks <seth@subimage.com>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] insert default into serial fields?
Date: Tue, 07 Mar 2000 03:36:33 -0500
Message-ID: <19500.952418193@sss.pgh.pa.us> (raw)
In-Reply-To: <NOEPJACOBMIOEOLHKIODMELDCBAA.seth@subimage.com>
References: <NOEPJACOBMIOEOLHKIODMELDCBAA.seth@subimage.com>
"Seth Banks" <seth@subimage.com> writes:
> what's the appropriate way to go about inserting values into a table with a
> serial field?
> i have tried '', 'default', and default...but none seem to work correctly.
> any help?
INSERT ... VALUES(..., DEFAULT, ...) ought to work according to SQL92,
but we don't support it currently. I recommend using an explicit
column list in INSERT:
INSERT INTO table(col1,col2,col4) VALUES(val1,val2,val4)
where you omit the serial column's name (col3, maybe, in this example).
Another way is to explicitly give the same expression as the column's
default value: nextval('sequence-object-name'). But that's pretty
ugly because it ties your code to the current implementation of SERIAL.
One great advantage of explicitly listing the column names is that
your INSERT code doesn't break if you add or reorder columns in the
table declaration. IMHO that makes up for the tedium of writing them
all out, but you might see it differently...
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, seth@subimage.com
Subject: Re: [SQL] insert default into serial fields?
In-Reply-To: <19500.952418193@sss.pgh.pa.us>
References: <NOEPJACOBMIOEOLHKIODMELDCBAA.seth@subimage.com> <19500.952418193@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox