Re: [SQL] INSERT w/o variable names for a SERIAL type? - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] INSERT w/o variable names for a SERIAL type?
Date
Msg-id 9907.951676161@sss.pgh.pa.us
Whole thread Raw
In response to Re: [SQL] INSERT w/o variable names for a SERIAL type?  (Peter Eisentraut <peter_e@gmx.net>)
Responses Re: [SQL] INSERT w/o variable names for a SERIAL type?
Re: [SQL] INSERT w/o variable names for a SERIAL type?
List pgsql-sql
Peter Eisentraut <peter_e@gmx.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


pgsql-sql by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: [SQL] INSERT w/o variable names for a SERIAL type?
Next
From: Bruce Momjian
Date:
Subject: Re: [SQL] INSERT w/o variable names for a SERIAL type?