If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Returning the "Auto-Increment" column value

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-04-09, 05:24
naresh_singam naresh_singam is offline
Registered User
 
Join Date: Apr 2008
Posts: 7
Returning the "Auto-Increment" column value

Hi,

I have table where one of the column is "Auto-Incremented". Whenever I insert data into that table the query should retuun the value of the Auto-Incremented column. So that I can use that value in my further queries. Please help to achieve this functionality.

Thanks,
Naresh.
Reply With Quote
  #2 (permalink)  
Old 02-04-09, 05:55
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Code:
select mysql_insert_id()
The documentation is here

Mike
Reply With Quote
  #3 (permalink)  
Old 02-04-09, 08:05
naresh_singam naresh_singam is offline
Registered User
 
Join Date: Apr 2008
Posts: 7
Thanks Mike for your response,

But my requirement is I should not call the any function or query again. Along with the insert query inself the value should return.

My actual requirement is :
I have two tables (parent and child) when ever I call insert method, first I will insert data into the parent table and then I should insert data into the child table where I required the "Auto-Increment" column value to be inserted in the child table.
If I use function to get the latest id I will have problem when multiple users uses my application.

Thanks,
Naresh.
Reply With Quote
  #4 (permalink)  
Old 02-04-09, 08:36
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
You could try something like this :
Code:
begin transaction
   insert parent ( id, parent_data ) values ( null, "parent_data" );
   insert child ( id, parent_id, child_data ) values( null, mysql_insert_id(), "child_data" );
commit transaction
I don't know anything about your application but I think a function wouldn't be a bad idea for inserting the parent. The function would insert the parent and then return the id of the parent. Each process would use this value when adding it's children. Also if there is little difference between a parent and a child I'd say use the same table and hence the same function to add these as well.

Code:
select add_item( null, "parent_data" ) into v_parent_id;
select add_item( v_parent_id, "child_data_1" );
select add_item( v_parent_id, "child_data_2" );
Reply With Quote
  #5 (permalink)  
Old 02-04-09, 13:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by naresh_singam
If I use function to get the latest id I will have problem when multiple users uses my application.
no, this is not true

mysql keeps track of the last auto_increment value per connection

use the mysql_insert_id php function if you're using php, otherwise use the LAST_INSERT_ID mysql function
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On