mysql - best way to provide unique voucher I am designing a website that will provide vouchers (serial codes). All voucher codes generated and saved in a table like this and not generated online:

 v_id             voucher                status (used?)
-------------------------------------------------------
  1              abcdefgabcdefg1             0
  2              abcdefgabcdefg2             0
  3              abcdefgabcdefg3             0
  4              abcdefgabcdefg4             0


I should display unused (status:0) voucher to end user after successful payment but I am not sure how can I do this because this site may have high traffic so more than one people may visit site in same milisecond of time, so for example I can not just use:

SELECT * FROM table WHERE status=0 LIMIT 0,1;


and then UPDATE it:

UPDATE table SET status=1 WHERE v_id=....


because two people may visit in same time so we provide same serial code in SELECT before we UPDATE.

what would be best way to ensure I display unique serial code?
One way to do it is to update and select in the same statement; and add a condition to the statement so that (if selected, don't select again)

Here is my way:

SET @voucher_id=0;
UPDATE voucher_table SET status=1, v_id = (SELECT @voucher_id := v_id) WHERE status=0 LIMIT 1;
SELECT * FROM voucher_table WHERE v_id=@voucher_id;


It is kind of creating an artificial lock. Notice that any other transaction (or user) won't be able to pick up the same voucher ID due to the Atomicity and Isolation in ACID properties of the database systems.