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_INCREMENTvalue generated for most recent statement affecting an
AUTO_INCREMENTcolumn by that client. This value cannot be affected by other clients, even if they generate
AUTO_INCREMENTvalues 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.
So, I've spent years panicking about race conditions in code like that below that apparently won't exist (at least in my cases):
`tbl` ------------------------ `id` INT AUTO_INCREMENT `col` TEXT INSERT INTO `tbl` (`col`) VALUES("lol"); SELECT LAST_INSERT_ID(); -- get `id` for inserted row
Perhaps you have, too?
This is not explicitly the case for
FOUND_ROWS(), though the following paragraph loosely implies that
FOUND_ROWS() is guaranteed to be ok after
SQL_CALC_FOUND_ROWS, at least within the same connection context:
The row count available through
FOUND_ROWS()is transient and not intended to be available past the statement following the