Get Autoincrement value after INSERT query in MySQL

Lot of time we have requirement to update two tables simultaneously. Say for example, we have two tables CUSTOMER_DETAILS and CUSTOMER_ADDRESS. While adding a row in CUSTOMER_DETAILS, few details like address is first written in CUSTOMER_ADDRESS and its reference is added in CUSTOMER_DETAILS table. Now if ADD_ID is a primary key in CUSTOMER_ADDRESS table and if it is an auto increment field than how to add this key as a foreign key in CUSTOMER_DETAILS table?

Well, check following queries:

INSERT INTO CUSTOMER_ADDRESS (ADD_ID, ADD_TEXT) VALUES(NULL, 'some address value');

INSERT INTO CUSTOMER_DETAILS (NAME, ADD_ID, GENDER, PHONE_NO)
VALUES ('James Bond', LAST_INSERT_ID(), 'MALE', 007);

Now when the first query will get executed, address details will be added in CUSTOMER_ADDRESS table and the ADD_ID will be updated based on autoincrement field as we passed NULL in its place. Just after the completion of first query, we want to add custormer details in CUSTOMER_DETAILS table where we will need the ADD_ID that we just added in CUSTOMER_ADDRESS. Now note that we have used LAST_INSERT_ID() function to retrieve the latest autoincrement ID that was used in CUSTOMER_ADDRESS table.

Thus, LAST_INSERT_ID() can be used to fetch latest autoincrement id being used in any INSERT query that was just got executed.

Also, you can fetch the recently added autoincrement ID by using following SELECT query:

SELECT LAST_INSERT_ID();


12 Comments

  • Steven wrote on 4 December, 2008, 7:58

    doesnt sound very thread safe to me

  • Viral Patel wrote on 4 December, 2008, 9:35

    Hi Steven,
    LAST_INSERT_ID() is unique per session. The initial value will be 0. If multiple threads are using same connection then there may arise some concurrency issues. But for different sessions it will be fine.
    Read TIP 9: at http://kerneltrap.org/node/3096

  • Akshay wrote on 6 January, 2009, 23:35

    Hi,
    Thanks for the tip but i’ve got 1 question:
    In a multi-connection situation, wouldn’t the last_insert_id() changed ,’incremented’, BEFORE the next “insert ” query is executed?

    As such foreign key will be wrong for the customer details.
    Am i wrong?

  • Viral wrote on 7 January, 2009, 10:51

    Hi Akshay,
    As I have mentioned in the above mail, LAST_INSERT_ID() is unique to the login session. Go through the link: http://kerneltrap.org/node/3096

  • web wrote on 25 March, 2009, 13:53

    I hope this works

  • pravin wrote on 8 August, 2009, 17:25

    viral bhai,
    hu drupal par work karu chhu.mare ek field autoincrement karvi chhe runtime.
    means jyare hu data enter karu ane database maa feed karu to next time unique field autoincrement thavi joye.

  • rahu wrote on 7 September, 2009, 22:28

    very helpful tutorial which solved my sql trouble. thanks

  • Carlo wrote on 29 January, 2010, 1:07

    Hi Viral, maybe is a dumb question, but what do you mean with session?
    I have a conn.php and all my php pages connect to the database through this connector.

    But what happened if I make an insert and immediately I use the LAST_INSERT_ID but hundreds or thousands of users are inserting rows in this same millisecond?

    I want to do something like this but I don’t know is safe:

    $insert1 = “INSERT INTO table1 (id_table1, description) VAUES (”,’$var_here’)”
    $insert2 = “INSERT INTO table2 (id_table2, id_table1) VALUES (”,LAST_INSERT_ID)”

    Thanks in advance

  • Ram.. wrote on 1 May, 2010, 21:43

    Hello Viral,

    Nice article i got the solution for my problem, thanks..

  • Lenin wrote on 11 September, 2010, 0:17

    HI, The correct function is mysql_insert_id(); not what you mentioned in this post….

  • plr store wrote on 11 November, 2010, 22:19

    i second that, mysql_insert_id(); works for me

  • Neo wrote on 13 May, 2011, 18:14

    SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = $dbName AND TABLE_NAME = $tblName

Leave a Reply

Your email address will not be published. Required fields are marked *

*