NYCPHP Meetup

NYPHP.org

[nycphp-talk] getting last inserted id with PostgreSQL

Mitch Pirtle mitch.pirtle at gmail.com
Sat Nov 20 13:31:40 EST 2004


On Sat, 20 Nov 2004 12:42:27 -0500 (EST), Jayesh Sheth
<jayeshsh at ceruleansky.com> wrote:
> In response to your question about getting the last id inserted into a
> table (on a per connection basis), these documents may be relevant:
> 
> http://archives.postgresql.org/pgsql-general/2003-08/msg01699.php
> http://www.postgresql.org/docs/7.3/static/functions-sequence.html
> 
> I have not used PostgreSQL myself yet, but as far as I can tell, these
> documents may help. More specifically, it is the "currval" part you should
> be looking at.

>From what I've seen, there's really only two ways to do this - the
MySQL way (using auto_increment) and the non-MySQL way (using sequence
values).

I always thought the MySQL way was the simplest and easiest to use
from a programmatic perspective, but the more time I worked on web
applications (weblications?) the more I came to appreciate sequences.

When you get the value from a sequence, it immediately increments
itself.  This is also a singular transaction, which ensures that only
you will get that value from the sequence.

With MySQL and auto_increment, there is the possibility for an insert
to happen before you get your value, correct?

Not a dig at MySQL, just wanting clarification on the atomicity of
doing an insert and then getting the last value from the
auto_increment fields...

-- Mitch



More information about the talk mailing list