在MySQL中,update user set id=?,name=? where id=? 语句在并发情况下,会出现‘锁’的现象吗?

或者update user set name=?,sex=? where name=? 这种情况
2025-03-10 12:22:25
推荐回答(1个)
回答1:

update理论上都有锁,只要不死锁,就问题不大
如你在一个事务中
update user where userid=1;
update dept where deptid=2;
commit;

而另一个连接
update dept where deptid=2;
update user where userid=1;
commit;

如果这2个连接同时执行这些语句,就可能死锁。

所以要特别注意update的表的顺序和where 条件的中记录的执行顺序(对参数先排序)
1)
update user set ... where userid=1;
update user set ... where userid=2;
commit
2)
update user set ... where userid=2;
update user set ... where userid=1;
commit
可能死锁