Re: going crazy with serial type

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Gregory Wood" <gregw(at)com-stock(dot)com>
Cc: "Cindy" <ctmoore(at)uci(dot)edu>, "PostgreSQL-General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: going crazy with serial type
Date: 2002-01-31 20:33:34
Message-ID: 4990.1012509214@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Gregory Wood" <gregw(at)com-stock(dot)com> writes:
> [ lots of good advice snipped ]

> Or explicitly give the serial a NULL value:

> insert into mytable (NULL, 1, 2);
> insert into mytable (NULL, 5, 6);

Oops, struck out on your last at-bat :-(. The above will insert NULLs.

The *only* way to get the default value inserted is not to specify
the column at all in the insert. The way you suggested works:

> insert into mytable (a, b) VALUES (1, 2);

Another option is to arrange the columns of the table so that the
one(s) you typically default are at the end, and then you can leave
off the column name list in INSERT:

create table mytable (a int, b int, id serial);

insert into mytable values (1, 2);

However, a lot of people consider it good practice to explicitly write
out a column name list in every INSERT anyway. The above shortcut will
come back to bite you if you ever rearrange the columns again. An
INSERT with column names listed is relatively impervious to schema
rearrangements.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Cindy 2002-01-31 20:37:30 Re: going crazy with serial type
Previous Message Neil Conway 2002-01-31 20:27:14 Re: index does not improve performance