Skip site navigation (1) Skip section navigation (2)

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

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, 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 19:04:42
Message-ID: 200002271904.OAA01223@candle.pha.pa.us (view raw or flat)
Thread:
Lists: pgsql-sql
> > 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.

The problem is when you are inserting >50 columns, it is a pain.  The
use of DEFAULT would also allow SERIAL columns to get the proper
nextval(), rather than having specify the nextval() call specifically.

Added to TODO.

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman(at)candle(dot)pha(dot)pa(dot)us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

In response to

Responses

pgsql-sql by date

Next:From: Ross J. ReedstromDate: 2000-02-27 19:44:02
Subject: Re: [SQL] INSERT w/o variable names for a SERIAL type?
Previous:From: Bruce MomjianDate: 2000-02-27 18:58:43
Subject: Re: [SQL] INSERT w/o variable names for a SERIAL type?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group