Get the App
SLTechnology News&Howtos  ›  Database  › 

How to add Row Lock in mysql

Shulou Source: shulou.com Published: 2022-06-01 03:26:15 10月26日 Update

Editor to share with you how to add mysql row lock, I hope you will learn a lot after reading this article, let's discuss it together!

Create a row lock condition:

1. Create an index in the table, select. The where field (must be an index) or the row lock is invalid.

2. There must be a transaction, which is the row lock (exclusive lock).

3. Add FOR UPDATE after the select statement

Example: to test whether a lock is added, execute 1 and then 2

Query 1

START TRANSACTION; # plus transaction SELECT UserID,Password,Age FROM AccountsDB.Accounts_InFo WHERE Accounts = 'hello2' FOR UPDATE; # locked select SLEEP (5); # Sleep COMMIT; # commit

Query 2:

START TRANSACTION; SELECT UserID,Password,Age FROM AccountsDB.Accounts_InFo WHERE Accounts = 'hello2' FOR UPDATE;COMMIT; after reading this article, I believe you have a certain understanding of mysql how to add row locks, want to know more about it, welcome to follow the industry information channel, thank you for reading!

Tags: Add lines transactions articles indexes queries examples fields done more conditions knowledge industry sentences information information channels channels medium innovation testing sleep Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker Redmi Apple Xiaomi