Lets say the last row in my orders table is
Order_ID Product
555 Apple
How can I make sure that 2 orders happening at the exact time wont get the same same Order_ID i.e 556 for the next order because this is what's happening to me. My table uses INNODB.
Actually the Order_ID is an auto-increment Field. And I am using the Show Table Status query and using the [Auto-increment] to get the next Order_iD. Any suggestions?
解决方案
Define the Order_ID column with the AUTO_INCREMENT attribute and let MySQL assign values for you (that you then retrieve by asking the driver for the last_insert_id after a successful insertion):
ALTER TABLE orders MOFIFY Order_ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT;
Then, to insert records you simply don't provide an Order_ID value and it will automatically be assigned:
INSERT INTO orders (Product) VALUES ('Apple');