mysql 多线程 不重复,如何保证并发线程在MYSQL中不会获得相同的值?

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');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值