Home > Database > Mysql Tutorial > body text

How to Achieve Row-Level Locking in MySQL\'s MyISAM Tables?

Barbara Streisand
Release: 2024-10-31 11:25:29
Original
690 people have browsed it

How to Achieve Row-Level Locking in MySQL's MyISAM Tables?

Locking a Single Row in MyISAM Table

In MySQL, MyISAM tables do not support row-level locking. However, if you require row-level locking capabilities, you can consider switching to InnoDB, which provides this feature.

Switching to InnoDB for Row-Level Locking

To enable row-level locking, you need to convert your user table from MyISAM to InnoDB. You can do this using the following command:

ALTER TABLE <user_table> ENGINE=InnoDB;
Copy after login

Once you have converted to InnoDB, you can use the SELECT ... FOR UPDATE statement to obtain an exclusive lock on a particular row. This will prevent other sessions from modifying or accessing the locked row until the lock is released.

Here's an example:

SELECT * FROM <user_table> 
WHERE <condition> 
FOR UPDATE;
Copy after login

Note: You should release the lock promptly after you have completed your operations on the row. This can be done by issuing a COMMIT or ROLLBACK statement.

Additional Resources:

  • [MySQL Documentation: Locks Set by SQL Statements for InnoDB Tables](http://dev.mysql.com/doc/refman/5.0/en/innodb-locks-set.html)

The above is the detailed content of How to Achieve Row-Level Locking in MySQL\'s MyISAM Tables?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!