This question already has answers here:
LAST_INSERT_ID() MySQL
(14 answers)
Closed 8 years ago.
I didn't find an exact answer so I try to ask here.
I running an SQL INSERT Query and after that I want to use the ID of the
this INSERT Query, I found this: LAST_INSERT_ID() and it's work, but I thinking what would happen if I not the only one who run the query on the server I can get a wrong ID from the DB, so there is other to take the last ID ?
This is both an answer and a clarification of the misconceptions presented elsewhere in this Q&A.
After you do an INSERT into a table with an AUTO_INCREMENT, the value of that AI is waiting for you in that connection; you can fetch it via LAST_INSERT_ID (or whatever synonym your client's API has).
It is tied to the connection, not a transaction.
Being tied to the connection, no other connection can accidentally get the id that you just created.
MAX(id) does run the risk of another connection INSERTing another row, thereby bumping id beyond the value you just inserted. Hence, MAX(id) could give you the wrong value.
If you need to do INSERT..ON DUPLICATE UPDATE, the online manual has a clear example of how to get the id of the row, whether it was INSERTed or UPDATEd. It requires that you the kludgy looking id = LAST_INSERT_ID(id) in the UPDATE part.
The only requirement for an AUTO_INCREMENT be that it is the first column in some index, not necessarily the PRIMARY KEY. (MyISAM has an exception, for which InnoDB has no counterpart.) What you could lose by not saying PRIMARY KEY(id) is the prevention of duplicate ids when you explicitly INSERT the same id again.
It should return the last inserted row id from the same connection. So if other user has inserted another row in between, LAST_INSERT_ID function will still return the relevant idfor the first user.
Quoting from mysql documentation:
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function(LAST_INSERT_ID) to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client
Here is the link http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id .
you can get last id from your table like this it would be better if id is auto increment
Select max(id) from yourtable
Related
According to the PHP documentation, mysql_insert_id takes the last inserted id from the mysql table.
My question is, if I have a website that inserts more than 2 rows per second to the DB, can I use the mysql_insert_id and get the correct ID I am referring to in the INSERT query a line before?
From the MySQL manual:
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.
Short version: it is safe to use.
mysql_insert_id gives you the insert id of the most recent INSERT statement on the connection you give it.
If you call this immediatly after your insert, on the same mysql connection, you get the inserted id matching that insert statement, independantly of any other inserts going on in the mean time.
According to the PHP documentation, mysql_insert_id takes the last inserted id from the mysql table.
My question is, if I have a website that inserts more than 2 rows per second to the DB, can I use the mysql_insert_id and get the correct ID I am referring to in the INSERT query a line before?
From the MySQL manual:
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.
Short version: it is safe to use.
mysql_insert_id gives you the insert id of the most recent INSERT statement on the connection you give it.
If you call this immediatly after your insert, on the same mysql connection, you get the inserted id matching that insert statement, independantly of any other inserts going on in the mean time.
According to the PHP documentation, mysql_insert_id takes the last inserted id from the mysql table.
My question is, if I have a website that inserts more than 2 rows per second to the DB, can I use the mysql_insert_id and get the correct ID I am referring to in the INSERT query a line before?
From the MySQL manual:
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.
Short version: it is safe to use.
mysql_insert_id gives you the insert id of the most recent INSERT statement on the connection you give it.
If you call this immediatly after your insert, on the same mysql connection, you get the inserted id matching that insert statement, independantly of any other inserts going on in the mean time.
I want to write a function that returns the value of a column (in this case, an auto-incrementing primary key) for a row that it inserts.
Essentially, I want to insert some new data, have a new primary key generated, then get that key. I could simply look for the highest primary key in the table, but it is possible that someone else could be running the function as well, and I could return the wrong key, right?
What's the simplest way to negotiate this problem?
As pointed in the comments, from MySQL documentation:
mysql> SELECT LAST_INSERT_ID();
-> 195
This LAST_INSERT_ID() function is not subject to a race condition like SELECT MAX(id) might be, because it's maintained within MySQL specific to your connection. It returns the ID generated for your last insert, not anybody's last insert.
According to the PHP documentation, mysql_insert_id takes the last inserted id from the mysql table.
My question is, if I have a website that inserts more than 2 rows per second to the DB, can I use the mysql_insert_id and get the correct ID I am referring to in the INSERT query a line before?
From the MySQL manual:
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.
Short version: it is safe to use.
mysql_insert_id gives you the insert id of the most recent INSERT statement on the connection you give it.
If you call this immediatly after your insert, on the same mysql connection, you get the inserted id matching that insert statement, independantly of any other inserts going on in the mean time.