Will Update in a Mysql transaction lock the table?

Will Update in a Mysql transaction lock the table?

Two cases:

1. With index 2. Without index

Prerequisites:

Method: Use command line to simulate

1. Since MySQL automatically commits transactions by default, you must first check whether your current database has automatically committed transactions.

Command: select @@autocommit;

The results are as follows:

+--------------+
| @@autocommit |
+--------------+
| 0 |
+--------------+

If it is 1, run the command: set autocommit = 0; set to disable automatic commit

2. The current database table format is as follows

tb_user | CREATE TABLE `tb_user` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `name` varchar(32) DEFAULT NULL,
 `phone` varchar(11) DEFAULT NULL,
 `operator` varchar(32) DEFAULT NULL,
 `gmt_create` datetime DEFAULT NULL,
 `gmt_modified` datetime DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

Obviously, I didn't add any indexes except the primary key.

Practical example:

1. No Index

Run the command: begin; to start the transaction, and then run the command: update tb_user set phone=11 where name="c1"; to make the changes. Do not commit the transaction yet.

Open another window and run the command directly: update tb_user set phone=22 where name="c2"; You will find that the command is stuck. However, when the previous transaction is submitted through commit, the command will run normally and end, indicating that the table is locked.

2. Add an index to the name field

create index index_name on tb_user(name);

Then continue the operation as in step 1, that is, to open a transaction and run update tb_user set phone=11 where name="c1"; do not commit yet

Then run another command update tb_user set phone=22 where name="c2"; and you will find that the command will not get stuck, indicating that the table is not locked.

But if another update tb_user set phone=22 where name="c1"; updates the same row, it means the row is locked.

3. Summary

If there is no index, the update will lock the table. If an index is added, the row will be locked.

This is the end of this article about whether Update in MySQL transaction will lock the table. For more information about MySQL transaction Update locking table, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Non-standard implementation code for MySQL UPDATE statement
  • mysql update case update field value is not fixed operation
  • When modifying a record in MySQL, the update operation field = field + string
  • MySQL select results to perform update example tutorial
  • Detailed analysis of the syntax of Mysql update to modify multiple fields and
  • Record a pitfall of MySQL update statement update
  • Detailed example of MySQL joint table update data
  • Detailed explanation of the execution process of mysql update statement
  • Summary of Mysql update multi-table joint update method
  • Difference between MySQL update set and and

<<:  HTML markup language - table tag

>>:  Detailed explanation of overlay network in Docker

Recommend

Docker deploys mysql remote connection to solve 2003 problems

Connecting to MySQL Here I use navicat to connect...

Vue defines private filters and basic usage

The methods and concepts of private filters and g...

mysql 8.0.15 winx64 decompression version graphic installation tutorial

Every time after installing the system, I have to...

Teach you how to achieve vertical centering elegantly (recommended)

Preface There are many ways to center horizontall...

Design of pop-up windows and floating layers in web design

In the trend of gradual transition from tradition...

Example of using CSS to achieve floating effect when mouse moves over card

principle Set a shadow on the element when hoveri...

How to create an index on a join table in MySQL

This article introduces how to create an index on...

Color matching techniques and effect display for beauty and styling websites

Color is one of the most important elements for a...

Use vue to implement handwritten signature function

Personal implementation screenshots: Install: npm...

HTML table tag tutorial (11): horizontal alignment attribute ALIGN

In the horizontal direction, you can set the alig...

getdata table table data join mysql method

public function json_product_list($where, $order)...

MySQL account password modification method (summary)

Preface: In the daily use of the database, it is ...