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);
Code language: SQL (Structured Query Language) (sql)SELECT LAST_INSERT_ID();
Code language: SQL (Structured Query Language) (sql)Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
doesnt sound very thread safe to me
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
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?
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
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.
And this URL is not working http://kerneltrap.org/node/3096 please give me plantation
I hope this works
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.
very helpful tutorial which solved my sql trouble. thanks
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
Hello Viral,
Nice article i got the solution for my problem, thanks..
HI, The correct function is mysql_insert_id(); not what you mentioned in this post....