RE: Last ID

From: "Chuck Kimber" <chuckk(at)ext(dot)usu(dot)edu>
To: <pgsql-novice(at)postgresql(dot)org>
Subject: RE: Last ID
Date: 2001-03-02 18:33:58
Message-ID: HOEAJICPFIHKNHNCKLFBEEKHCOAA.chuckk@ext.usu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

> How can I get Last ID inserted ???
> The problem is for a multiuser(symultans) database. Exist a statment SQL
> witch get the last id inserted for a session ?

Ignoring the usefulness you may find using something like nextval, when I
insert something into a database I already know something about how to
locate what makes the inserted row unique. Either I have something like a
firstname, lastname, address or some combination of values I've just
inserted that are fairly unique when combined together.

So I probably did something like:

Insert Into MyTable
(firstname, lastname, address, city, state)
Values
('$MyFirstName', '$MyLastName', '$MyAddress', '$MyCity', '$MyState');

So after I do that, I just query the table again with something like:

Select max(UniqueID)
>From MyTable
Where
firstname = '$MyFirstName'
AND
lastname = '$MyLastName'
AND
address = '$MyAddress'

And I just add anything else in the "Where" clause that will give me more
uniqueness. And I obviously know these things because I just inserted them.
Using "max()" provides the newest UniqueID where those variables are true.

This only works though if you have some arrangement of variables that when
combined with max have a very high likely-hood of producing the unique id
you are looking for. If your variables are not very unique, and in a
multi-user environment they may or may not be, this won't be very effective
for you. If they were all numeric fields with a high chance of duplication,
and your users were all inserting them with rapid fire, using max may give
you something someone has inserted since your data insertion... Game Over.

-Chuck

In response to

  • Re: Last ID at 2001-03-02 16:33:34 from Brett W. McCoy

Browse pgsql-novice by date

  From Date Subject
Next Message Brett W. McCoy 2001-03-02 18:53:24 Re: Last ID
Previous Message Brett W. McCoy 2001-03-02 18:23:45 Re: Last ID