当前位置:网站首页>发现了一个 MySQL 的巨坑:update 更新别再用影响行数做判断了!!!
发现了一个 MySQL 的巨坑:update 更新别再用影响行数做判断了!!!
2022-07-01 06:36:00 【Java技术栈】
点击关注公众号,Java干货及时送达
来源:cnblogs.com/yjmyzz/p/13562182.html
业务系统中,使用update语句更新数据是再正常不过的场景,我们也经常通过update更新的行数,来做一些业务判断,类似下面的伪代码:(mybatis + mysql 场景)
if (xxxMapper.updateByPrimaryKeySelective(entity)>0){
//更新成功,做其它业务处理
}但是这里有一个坑,mysql中update影响行数>0是有条件的,假如有一张表:

里面只有一条记录(ID=1),我们用update更新一把ID=1的这条记录。另外,最新 MySQL 面试题整理好了,大家可以在Java面试库小程序在线刷题。

这一行成功更新,影响行数为1,理所当然,然后把这条update语句再执行一次:

这时候,返回的影响行数为0,也就是说,当待更新的记录与原始记录旧值相同时,mysql其实并不会做任何更新。。
换言之,如果上游传过来的数据,与数据库本身的旧值相等,没有变化时,update语句影响行数为0。这与另一种场景:"更新一条并不存在的记录,影响行数返回0" 无法区分。

结论:不要使用update语句的影响行数做重要的业务判断!


别用 System... 计时了,StopWatch 好用到爆!
关注Java技术栈看更多干货


获取 Spring Boot 实战笔记!
边栏推荐
- VS2019如何永久配置本地OpenCV4.5.5使用
- Student attendance system for C language course (big homework)
- 问题:OfficeException: failed to start and connect(二)
- Esp32 monitors the battery voltage with ULP when the battery is powered
- 启牛学堂合作的证券公司是哪家?开户安全吗?
- 在支付宝上买基金安全吗?哪里可以买基金
- rclone常用子命令中文解释
- C language course is provided with employee information management system (large operation)
- ESP32 ESP-IDF GPIO按键中断响应
- 考研目录链接
猜你喜欢

mysql学习

Grain Mall - environment (p1-p27)

Figure out the difference between event coordinates screenx, clientx, pagex and offsetx

Using fuseki query when there are multiple models in TDB

问题:OfficeException: failed to start and connect(二)

Problem: officeexception: failed to start and connect (III)

Spark入门(一篇就够了)

ESP32在电池供电时用ULP监测电池电压

How the esp32 deep sleep current is lower than 10uA

解决无法读取META-INF.services里面定义的类
随机推荐
mysql数据类型学习笔记
node中引入模块的原理
Summary of wechat official account embedded program to jump to wechat
K8s set up redis cluster
图解事件坐标screenX、clientX、pageX, offsetX的区别
Resttemplate use
[unity shader custom material panel part I]
Promise
The code generator has eliminated the styling of xxxx js as it exceeds the max of 500kb
Stored procedure learning notes
Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
On whether variables are thread safe
Requests module (requests)
[unity shader ablation effect _ case sharing]
记一次线上接口慢查询问题排查
下载外文期刊的方法
Jena基于OWL的默认推理查询
微信公众号内嵌跳转微信小程序方案总结
MySQL data type learning notes
Which securities company does qiniu school cooperate with? Is it safe to open an account?