Re: [SQL] INSERT w/o variable names for a SERIAL type?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Emils Klotins <emils(at)mail(dot)usis(dot)bkc(dot)lv>, pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] INSERT w/o variable names for a SERIAL type?
Date: 2000-02-27 18:29:21
Message-ID: 9907.951676161@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> Emils Klotins writes:
>> Now, if I want to write a general #define, I'd need to have a way
>> to specify 'default' for SERIAL field, for, if I omit the id field in
>> VALUES, I need to specify all the rest of the fields explicitly.
>>
>> Is there any value I could put in place of id in VALUES part, to
>> make it replaced with the next value in sequence?

> INSERT INTO my_table VALUES (a, b, c, DEFAULT, x, y, z, ...);

I think that is legal SQL92 syntax, but Postgres doesn't accept it
at present.

The usual recommendation is to call out the columns you are loading
explicitly:

INSERT INTO my_table(a,b,d) VALUES (val-for-a, val-for-b, val-for-d);

The ones you don't load get their default values substituted instead.

This way is a shade more verbose, but it's good solid defensive
programming practice: the insert will do what it's supposed to
even if the table schema changes to add/delete/reorder columns.

regards, tom lane

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Bruce Momjian 2000-02-27 18:58:43 Re: [SQL] INSERT w/o variable names for a SERIAL type?
Previous Message Peter Eisentraut 2000-02-27 15:18:39 Re: [SQL] INSERT w/o variable names for a SERIAL type?