Is there a way to restrict insert or update to specific columns in a table using innodb engine in MySQL? I want to update these columns only through triggers or procedures
What I want to do is to calculate these columns automatically and not allow the change of these directly. Let me know if my approach is not correct
Your approach is unusual, but not impossible. Whether it is “correct” will depend on factors not mentioned in your question:
If the columns are always the result of some simple expression, you could simply specify that expression in each
SELECT:Or store the results in a view so that you don’t need to write the expression(s) every time:
In which case, to select the results one merely need do:
However, note that
calculated_columnwill still be evaluated each time you select from the view, so could lead to performance problems if it’s a complicated expression or your table is large. If instead you want to store the result of the calculation (so that it need not be evaluated on eachSELECT), you could define triggers that overwrite any value supplied by the user:If the columns will actually be periodically updated, but only from a stored procedure:
Deny your existing user(s) permission to update the columns:
Note that if your user has table- or database-level privileges, they will need to be revoked and replaced with column-level ones as appropriate.
Create a user under which the procedure will run:
Define your procedure to run under that new user: