在MySQL中,下面update语句会出现‘锁’的现象吗

2025-05-08 04:47:03
推荐回答(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
可能死锁