RE: Last ID - Mailing list pgsql-novice

From Chuck Kimber
Subject RE: Last ID
Date
Msg-id HOEAJICPFIHKNHNCKLFBEEKHCOAA.chuckk@ext.usu.edu
Whole thread Raw
In response to Re: Last ID  ("Brett W. McCoy" <bmccoy@chapelperilous.net>)
List 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


pgsql-novice by date:

Previous
From: "Brett W. McCoy"
Date:
Subject: Re: Last ID
Next
From: "Brett W. McCoy"
Date:
Subject: Re: Last ID