Create another table with one row and column in which the next id value will be stored. Then create an insert trigger in the source table that increments the value in the second table, captures it, and uses the ID column in the first table. You need to be careful how you make choices and updates to ensure their atomicity.
Essentially, you are emulating an Oracle sequence in MySQL. This will lock one row in the sequence table, so it may make it inappropriate for what you are doing.
ETA:
Another similar, but possibly more effective option is to create a second "sequence" table, in which there will be only one PK column with automatic increment and no other data. Ask the trigger to insert a row into this table and use the generated identifier from there to populate the identifier in the original table. Then, either a trigger or another process periodically removes all rows from the sequence table to clear it.
Eric Petroelje
source share