我正在尝试增加表中的字段:
UID | AMOUNT
5 | 100
要做到这一点,还有很长的路要走:
select amount from table where uid = 5
update table set amount = 101 where uid = 5
但是,可能会出现并发问题。在执行更新之前,另一个进程可能已经更新了该字段。
而不是这个,以下不会解决我的问题:
update table set amount = amount + 1 where uid = 5
MySQL 是否会自行锁定该字段并确保不会产生并发副作用?
答案 0 :(得分:2)
也许这个链接会帮助你Does MySql automatically lock rows or do I need to add "lock in share mode"? 您还可以考虑创建如下所示的交易。
begin;
select amount from table where uid = 5 for update;
update table set amount = 101 where uid = 5;
commit;
for update
语句将锁定该行并防止其他事务读取该行。