当前位置:网站首页>In the process of enterprise development, I found that a colleague used the select * from where condition for update

In the process of enterprise development, I found that a colleague used the select * from where condition for update

2022-06-25 00:23:00 SteveCode.

Active address : Graduation season · The technique of attack er

In the workplace Introvert

In the process of enterprise development, I found that a colleague used select * from where Conditions for update

ok ! Today I learned my first hand Send a post by the way . Hey

My article for reference

Novice tutorial -MySQL Business

Interviewer asked :select…for update Will you lock the table or the row ?

Start bar ! Hey

Look at a paragraph sql, Feel if you are confused There may also be big men

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;

We explain one by one

  • Here we are. We'll talk about This transaction Holy and great . Either successful or unsuccessful . Principle 、 Uniformity 、 Isolation, 、 persistence . Wait, Ba , And the isolation level of transactions , also spring Propagation characteristics of transactions in They are all regular interview sites

BEGIN

Is to start a transaction

COMMIT;

Commit transaction

SELECT @@autocommit;

Check whether the transaction is statically auto committed

autocommit State value description
  • AUTOCOMMIT=0 Disable auto submit
  • AUTOCOMMIT=1 Turn on auto submit
    modify state :
  • SET AUTOCOMMIT=0 Disable auto submit
  • SET AUTOCOMMIT=1 Turn on auto submit
    MySQL By default, the database will be Automatically start the transaction

image.png

Here we are Don't let him commit transactions automatically Stop him first

SET @@autocommit =0;
SELECT @@autocommit;

image.png

The real Kung Fu begins Ha

  • Create table statement
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;

Let's take a look at the data in my previous table

image.png

First, check through the primary key

SELECT * FROM a WHERE `key`=1 FOR UPDATE;
 Execute this sql   I didn't  COMMIT; Ha 

image.png

In execution

UPDATE a SET `name`='songxy999' WHERE `key` =1;

Look up the table data

image.png

  • No change

Execute the statement that commits the transaction

COMMIT;

image.png

  • Is the lock OK or surface Let me try

image.png

  • Wrong report ;error

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-OPLNv5n5-1655994728162)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4af5d417b96646b0a4a1430460b7d594~tplv-k3u1fbpfcp-watermark.image?)]

Look at the picture

image.png

This shows that the lock line

Active address : Graduation season · The technique of attack er

原网站

版权声明
本文为[SteveCode.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206241948030784.html