Re: going crazy with serial type - Mailing list pgsql-general

From Tom Lane
Subject Re: going crazy with serial type
Date
Msg-id 4990.1012509214@sss.pgh.pa.us
Whole thread Raw
In response to Re: going crazy with serial type  ("Gregory Wood" <gregw@com-stock.com>)
List pgsql-general
"Gregory Wood" <gregw@com-stock.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

pgsql-general by date:

Previous
From: Darren Ferguson
Date:
Subject: Re: unique & update
Next
From: Tom Lane
Date:
Subject: Re: going crazy with serial type