Re: sequence primary key

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jason Earl <jdearl(at)yahoo(dot)com>
Cc: Virginie Garcia <Virginie(dot)Garcia(at)pmtg(dot)u-bordeaux2(dot)fr>, pgsql-novice(at)postgresql(dot)org
Subject: Re: sequence primary key
Date: 2001-07-17 20:42:25
Message-ID: 26398.995402545@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Jason Earl <jdearl(at)yahoo(dot)com> writes:
> ... you need to make sure that you don't specify a
> toto_id in your insert query. For example your query
> should look something like:

> INSERT INTO toto (db) VALUES ('SOME VALUE')

> And then when you select from the table you will find
> that the toto_id column was filled automagically.

> processdata=> SELECT * FROM toto;
> toto_id | db
> ---------+------------
> 1 | SOME VALUE
> (1 row)

One way to make this easier is to put the columns that are normally
filled by a DEFAULT at the end of the table, not the start. For
example, if you'd made "db" the first column then it'd have sufficed
to write

INSERT INTO toto VALUES ('SOME VALUE')

with the same results as above.

However, a lot of people say it is good practice to write an explicit
list of the column names you are supplying in *every* INSERT, whether
you think you are skipping over defaulted columns or not. This makes
your code less likely to break when you rearrange the database layout.

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Scott Muir 2001-07-18 02:07:33 Iterations in a SELECT
Previous Message Jason Earl 2001-07-17 18:21:19 Re: sequence primary key