The only way to generate an auto-increment value is to try pasting. But you can cancel this transaction and still read the generated identifier. In MySQL 5.1 and later, the default behavior is that auto-increment values โโdo not get returned to the stack when rolling back.
START TRANSACTION; INSERT INTO mytable () VALUES (); ROLLBACK; SELECT LAST_INSERT_ID() INTO @my_ai_value;
Now you can be sure that no other transaction will try to use this value, so you can use it in your external processes, and then finally insert the value manually, which uses this id value (when you insert a specific id value, MySQL does not generates a new value).
source share