I have a row in a table that I do not want to be changed (ever).
Is it possible to set a MySQL row to READ-ONLY so that it cannot be updated in any way? If so, how?
If not, is it possible to set a permanent value in one of the columns of that row so that it cannot be changed? If so, how?
Thanks.
This is likely to be business logic, which probably doesn’t belong in your data storage layer. However, it can nonetheless be accomplished using triggers.
You can create a
BEFORE UPDATEtrigger that raises an error if a “locked” record is about to be updated; since an error occurs before the operation is undertaken, MySQL ceases to proceed with it. If you also want to prevent the record from being deleted, you’d need to create a similar triggerBEFORE DELETE.To determine whether a record is “locked”, you could create a boolean
lockedcolumn:Note that
SIGNALwas introduced in MySQL 5.5. In earlier versions, you must perform some erroneous action that causes MySQL to raise an error: I often call an non-existent procedure, e.g. withCALL raise_error;Again, if you absolutely must place this logic in the storage layer—and cannot identify the locked records through any means other than the PK—you could hard-code the test into your trigger; for example, to “lock” the record with
id_column = 1234:But this is absolutely horrible and I would do almost anything to avoid it whenever possible.