当前位置:网站首页>在企业级开发过程中我发现有位同事用select * from where 条件 for update
在企业级开发过程中我发现有位同事用select * from where 条件 for update
2022-06-24 19:50:00 【SteveCode.】
活动地址:毕业季·进击的技术er
职场 内卷人
在企业级开发过程中我发现有位同事用select * from where 条件 for update
好吧! 今天我就学习了一手 顺便发个帖子。嘿嘿
本人借鉴的文章
面试官问:select…for update会锁表还是锁行?
开始巴!嘿嘿
看一段sql,感觉一下是不是很懵逼 可能也会有大佬
BEGIN;
SELECT * FROM a WHERE `key`=1 FOR UPDATE;
UPDATE a SET `name`='songxy9991' WHERE `key` =1;
COMMIT;
SELECT @@autocommit;
SET @@autocommit =0;
SELECT * FROM a WHERE `key`=1 FOR UPDATE;
COMMIT;
UPDATE a SET `name`='songxy12' WHERE `key` =1;
我们一个一个的说明
- 讲到这里 我们就会说到 事务这个 神圣而又伟大。要么成功要么都不成功。原则性、一致性、隔离性、持久性。等等巴,还有事务的隔离级别,还有spring 中的事务的传播特性 都是面试常考点
BEGIN
就是开启事务
COMMIT;
提交事务
SELECT @@autocommit;
查看事务是否静止自动提交事务
autocommit 状态值说明
- AUTOCOMMIT=0 禁止自动提交
- AUTOCOMMIT=1 开启自动提交
修改状态: - SET AUTOCOMMIT=0 禁止自动提交
- SET AUTOCOMMIT=1 开启自动提交
MySQL数据库默认会是 自动开启事务的

这边我们就 不让他自动提交事务 给他先禁止掉
SET @@autocommit =0;
SELECT @@autocommit;

真功夫开始了 哈
- 建表语句
DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
`key` int(0) NOT NULL AUTO_INCREMENT,
`value` char(225) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`key`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of a
-- ----------------------------
INSERT INTO `a` VALUES (1, 'ssss', 'songxy12');
INSERT INTO `a` VALUES (2, 'yy', 'songxy');
INSERT INTO `a` VALUES (3, 'sxy', 'songxy');
INSERT INTO `a` VALUES (4, '1111', 'songxy');
INSERT INTO `a` VALUES (5, 'qqq', 'songxy');
SET FOREIGN_KEY_CHECKS = 1;
先看一下我之前表数据

先通过主键查
SELECT * FROM a WHERE `key`=1 FOR UPDATE;
执行这个sql 我可没有 COMMIT;哈

在执行
UPDATE a SET `name`='songxy999' WHERE `key` =1;
去查表数据

- 没有发生改变
执行提交事务的语句
COMMIT;

- 到底锁的行还是 表 我来试试

- 报错了;error
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OPLNv5n5-1655994728162)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4af5d417b96646b0a4a1430460b7d594~tplv-k3u1fbpfcp-watermark.image?)]
看图

由此可见是锁行
活动地址:毕业季·进击的技术er
边栏推荐
- Requests Library
- Jar package merging using Apache ant
- Color gradient gradient color collection
- How does VR panorama make money? Based on the objective analysis of the market from two aspects
- 中低速航空航天电子总线概述
- JDBC - database connection
- OTT营销之风正盛,商家到底该怎么投?
- 融合模型权限管理设计方案
- Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
- Ansible及playbook的相关操作
猜你喜欢

【排行榜】Carla leaderboard 排行榜 运行与参与手把手教学

Tape SVG animation JS effect
![[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL](/img/95/02a58c9dc97bd8347b43247e38357d.png)
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL

Ten commandments of self-learning in machine learning

Collective example

Fast pace? high pressure? VR panoramic Inn brings you a comfortable life

为什么生命科学企业都在陆续上云?

2019 summary and 2020 outlook

How to delete the entire row with duplicate items in a column of WPS table
Paper review: U2 net, u-net composed of u-net
随机推荐
Use and click of multitypeadapter in recycleview
Hibernate learning 2 - lazy loading (delayed loading), dynamic SQL parameters, caching
Fast pace? high pressure? VR panoramic Inn brings you a comfortable life
Use of JMeter
JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
Intensive reading of thinking about markdown
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
JS dynamically generates variable names and assigns values
颜色渐变梯度颜色集合
Current situation and development prospect forecast report of global and Chinese tetrahydrofurfuryl alcohol acetate industry from 2022 to 2028
Current situation analysis and development trend prediction report of hesperidase industry in the world and China from 2022 to 2028
Unimportant tokens can be stopped in advance! NVIDIA proposes an efficient visual transformer network a-vit with adaptive token to improve the throughput of the model
【面试题】instancof和getClass()的区别
Common redis commands in Linux system
Creative SVG ring clock JS effect
Adding, deleting, modifying and checking in low build code
5G dtu无线通信模块的电力应用
Decoupling pages and components using lifecycle
Analysis report on development trend and investment forecast of global and Chinese D-leucine industry from 2022 to 2028
Interesting checkbox counters