[UPHPU] Postgres Insert ID
Roberto Mello
roberto.mello at gmail.com
Wed Oct 22 17:29:05 MDT 2008
On Wed, Oct 22, 2008 at 5:00 PM, Joseph Scott <joseph at randomnetworks.com>wrote:
> It's just done differently. Instead of making the auto number feature a
> column attribute, it's done as a separate object (called a sequence).
>
<snip>
>
> In PostgreSQL (and any other DB that uses sequences, like Oracle) the order
> would be switched:
>
> - Ask for a new auto number ID from the sequence
> - Insert the new data, using the ID we got from the sequence
>
> That said it's pretty common in PostgreSQL to make an auto number field
> who's default value is to select the next number from the sequence.
>
Alternatively, you can define your PostgreSQL tables with columns of type
"serial", which just creates a sequence, and makes that column's default
value be the next value in the created sequence.
Then you can do your INSERT statements use the RETURNING keyword[1] and --
assuming the PHP driver knows how to deal with that -- grab the output
normally.
INSERT INTO mytable (id, foo, bar, ham spam)
VALUES (DEFAULT, 'blah', 'bleh', 'pizza', 'pizza')
RETURNING id;
Roberto
--
http://blog.divisiblebyfour.org/
More information about the UPHPU
mailing list