Home  >  Article  >  Database  >  How to Achieve Row-Level Locking in MySQL\'s MyISAM Tables?

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

Barbara Streisand
Barbara StreisandOriginal
2024-10-31 11:25:29692browse

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;

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;

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!

Statement:
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