简体   繁体   中英

How to put a long-term lock on a row in a MySQL InnoDB Table?

I've searched this for a while and couldn't find much.

Is it possible to lock individual rows manually on an InnoDB table in MySQL?

Basically we have a table where some records are processed and should no longer be edited. They need to be kept in the state they're currently in, but still be readily accessible in terms of reading the data.

We already have a "locked" column which is 0 or 1, but somehow , it's being edited when it shouldn't be. Rather than relying on the code to behave nicely, we were wanting to put a hard lock on it at a MySQL level, so these can't be changed, even if our PHP code tries to somewhere.

Is this possible?

No.

To do this correctly, use application partitioning such that your application stores active data in one table and historical data in another, and use permissions to specify that your application can read one table and read/write on another.

Alternatively, add an access layer so that you read through views and write through stored procedures, and have the SP control whether a row can be written based on the field value.

Either way you can't do this with MySQL natively without writing some code somewhere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM