While views are predominantly used for data retrieval, MySQL allows certain views to be updatable. An updatable view permits INSERT, UPDATE, and DELETE statements executed against the view to be directly translated to modifications on the underlying base table.
To prevent updates or inserts that violate the view's qualifying filtering criteria, MySQL provides the WITH CHECK OPTION constraint.
Rules for Updatable Views in MySQL
For a view to be updatable, there must be a direct 1-to-1 relationship between rows in the view and rows in the base table. A view is NOT updatable if its definition contains any of the following:
15WHERE department ='Engineering'ANDstatus='active';
Now, issuing an update directly through the view:
1UPDATE v_engineering_staff
2SET salary = salary *1.08
3WHERE emp_id =101;
This successfully modifies the salary column in the underlying employees table for employee 101!
The Risk Without WITH CHECK OPTION
Consider what happens if an update changes the filtering column:
1UPDATE v_engineering_staff
2SET department ='Marketing'
3WHERE emp_id =101;
Without protection, MySQL executes this update on employees. However, employee 101 is now in 'Marketing', meaning they instantly "vanish" from v_engineering_staff! Even worse, an INSERT could insert a record with department = 'Finance' through v_engineering_staff.
Enforcing WITH CHECK OPTION
By appending WITH CHECK OPTION, MySQL validates any inserted or updated row against the view's WHERE condition: