The commentary on the explanations indicates that you are interested in LAST_INSERT_ID () not giving the wrong result if another simultaneous INSERT happens. Be sure that it is safe to use LAST_INSERT_ID () regardless of other simultaneous activity. LAST_INSERT_ID () returns only the most recent identifier generated during the current session.
You can try it yourself:
- Open two shell windows, start the mysql client in each and connect to the database.
- Shell 1: INSERT to table with AUTO_INCREMENT.
- Shell 1: SELECT LAST_INSERT_ID (), see the result.
- Shell 2: INSERT to the same table.
- Shell 2: SELECT LAST_INSERT_ID (), see result different from shell 1.
- Shell 1: SELECT LAST_INSERT_ID () again, see the repeat earlier result.
If you think about it, this is the only way that makes sense. All databases that support auto-increment key mechanisms should act this way. If the result depends on the race condition with other clients, possibly at the same time as INSERTing, then there will be no reliable way to get the last inserted ID value in the current session.
Bill Karwin Oct 03 '08 at 0:42 2008-10-03 00:42
source share