Re: sequence primary key - Mailing list pgsql-novice

From Tom Lane
Subject Re: sequence primary key
Date
Msg-id 26398.995402545@sss.pgh.pa.us
Whole thread Raw
In response to Re: sequence primary key  (Jason Earl <jdearl@yahoo.com>)
List pgsql-novice
Jason Earl <jdearl@yahoo.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

pgsql-novice by date:

Previous
From: Jason Earl
Date:
Subject: Re: sequence primary key
Next
From: "Scott Muir"
Date:
Subject: Iterations in a SELECT