Unix Technical Forum

LAST_INSERT_ID()

This is a discussion on LAST_INSERT_ID() within the MySQL forums, part of the Database Server Software category; --> Hi all, I have a user table in a mysql db, when a user is inserted, I need the ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:05 AM
Gef.Mongoose@gmail.com
 
Posts: n/a
Default LAST_INSERT_ID()

Hi all,

I have a user table in a mysql db, when a user is inserted, I need the
generated keyfield from their entry to enter data into another table.
To accomplish this I insert the user and immediately after, run
another query using the LAST_INSERT_ID() command. This seems to work,
but isn't there a possibility that inbetween these two queries,
another user could register and the LAST_INSERT_ID return the wrong
id?
If so, is there a better way of doing this? I tried executescaler on
the insert, but it always returned nothing.

Any help would be appreciated!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:05 AM
Paul Lautman
 
Posts: n/a
Default Re: LAST_INSERT_ID()

Gef.Mongoose@gmail.com wrote:
> Hi all,
>
> I have a user table in a mysql db, when a user is inserted, I need the
> generated keyfield from their entry to enter data into another table.
> To accomplish this I insert the user and immediately after, run
> another query using the LAST_INSERT_ID() command. This seems to work,
> but isn't there a possibility that inbetween these two queries,
> another user could register and the LAST_INSERT_ID return the wrong
> id?
> If so, is there a better way of doing this? I tried executescaler on
> the insert, but it always returned nothing.
>
> Any help would be appreciated!


LAST_INSERT_ID() is connection specific. You will get the value from your
insert whilst someone else would get the value from their insert.

Indeed the book commonly known as the MySQL Reference Manual, has this to
say on the subject:

The ID that was generated is maintained in the server on a per-connection
basis. This means that the value returned by the function to a given client
is the first AUTO_INCREMENT value generated for most recent statement
affecting an AUTO_INCREMENT column by that client. This value cannot be
affected by other clients, even if they generate AUTO_INCREMENT values of
their own. This behavior ensures that each client can retrieve its own ID
without concern for the activity of other clients, and without the need for
locks or transactions.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:05 AM
Gef.Mongoose@gmail.com
 
Posts: n/a
Default Re: LAST_INSERT_ID()

On 4 Feb, 18:08, "Paul Lautman" <paul.laut...@btinternet.com> wrote:
> Gef.Mongo...@gmail.com wrote:
> > Hi all,

>
> > I have a user table in a mysql db, when a user is inserted, I need the
> > generated keyfield from their entry to enter data into another table.
> > To accomplish this I insert the user and immediately after, run
> > another query using the LAST_INSERT_ID() command. This seems to work,
> > but isn't there a possibility that inbetween these two queries,
> > another user could register and the LAST_INSERT_ID return the wrong
> > id?
> > If so, is there a better way of doing this? I tried executescaler on
> > the insert, but it always returned nothing.

>
> > Any help would be appreciated!

>
> LAST_INSERT_ID() is connection specific. You will get the value from your
> insert whilst someone else would get the value from their insert.
>
> Indeed the book commonly known as the MySQL Reference Manual, has this to
> say on the subject:
>
> The ID that was generated is maintained in the server on a per-connection
> basis. This means that the value returned by the function to a given client
> is the first AUTO_INCREMENT value generated for most recent statement
> affecting an AUTO_INCREMENT column by that client. This value cannot be
> affected by other clients, even if they generate AUTO_INCREMENT values of
> their own. This behavior ensures that each client can retrieve its own ID
> without concern for the activity of other clients, and without the need for
> locks or transactions.


Thanks for answering this. I'm using c# asp.net with mysql and am
curious if connection pooling utilised by .net would effect this
command in any way?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:22 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com