I have a database table which gets updated by a process from time to time. However, in some cases, I would like to enforce overrides to specific rows. Is there a way that I can without writing a lot of code to handle overrides, etc, just lock a row or field in the database itself?
For example, my table looks like
STUDENT | TEST_SCORE_AVG | HOMEWORK_AVG
John | 92.3 | 88.8
Lisa | 77.4 | 99.8
Terry | 88.0 | 64.5
With every new test or homework assignment, the student’s averages will change. However, say the teacher is John’s mother, and no matter what, he gets a 100.0 on his homework average. Is there a way to lock that in so that any program trying to change the value will get bounced back? I am using MySQL workbench to access my MySQL database (InnoDB)
UPDATE:
I could also make due with a function that locks the full row – meaning that neither TEST_SCORE_AVG nor HOMEWORK_AVG could ever be changed. I need the solution to persist over a long period of time, regardless of if the connection is lost. I want to lock the row until such time that I otherwise unlock it
No, there’s no practical way to prevent a single value in a single row from ever changing.
Using just about anything besides MySQL, you could create a CHECK() constraint that guarantees John’s homework average is always 100, but I wouldn’t call that practical. On MySQL, you’d have to use a trigger to get the same feature, but again I wouldn’t call that practical.
Other answers refer to row-level locking, which really has nothing to do with preventing a single value in a single row from changing.