Sub topic 8
Keeping Writes Inside the View with `WITH CHECK OPTION`
Prerequisites and the problem it solves
Assume you already know SELECT, WHERE, JOIN, CREATE VIEW, and basic database privileges. This chapter focuses on views that accept INSERT, UPDATE, or DELETE.
An updatable view is a view through which the database can change rows in its underlying table. For example, this view exposes only active employees:
CREATE VIEW active_employees AS
SELECT id, name, salary, status, department_id
FROM employees
WHERE status = 'active';The WHERE clause controls which rows the view displays. However, without an additional rule, it might not control which rows users can create or change through the view.
For example, this statement might succeed:
INSERT INTO active_employees
(id, name, salary, status, department_id)
VALUES
(10, 'Ben', 4800, 'inactive', NULL);The new row does not satisfy status = 'active', so it will not appear in active_employees. The insert can therefore create a row through a view that immediately hides that row.
Adding `WITH CHECK OPTION`
WITH CHECK OPTION tells the database to check every INSERT and UPDATE made through the view. The changed row must satisfy the view's query condition.
CREATE VIEW active_employees AS
SELECT id, name, salary, status, department_id
FROM employees
WHERE status = 'active'
WITH CHECK OPTION;Now an insert that produces an inactive employee fails:
INSERT INTO active_employees
(id, name, salary, status, department_id)
VALUES
(11, 'Casey', 5200, 'inactive', NULL);The database rejects the statement because the new row would not be visible through active_employees.
An insert that satisfies the condition succeeds:
INSERT INTO active_employees
(id, name, salary, status, department_id)
VALUES
(12, 'Drew', 5100, 'active', NULL);The same rule applies when updating an existing row through the view:
UPDATE active_employees
SET status = 'inactive'
WHERE id = 12;This update fails because it would move the row outside the view's result.
What the option does not guarantee
WITH CHECK OPTION does not make every view updatable. Views containing joins, grouping, DISTINCT, aggregates, or set operations often cannot accept direct writes, depending on the database system.
The view must first be writable. Then WITH CHECK OPTION adds a visibility condition to writes that the database already knows how to perform.
Database systems also differ in their exact support and error messages. Test the syntax and updatability rules for your database engine before relying on a view as a write interface.
A view built on another filtered view may inherit checks from the underlying view. This is useful for layered rules, but it can make failures harder to understand. Keep write paths simple when possible, and document which view conditions must remain true.
Exercise
What happens when this statement runs against an updatable view named activeemployees, defined with WHERE status = 'active' WITH CHECK OPTION? UPDATE activeemployees SET status = 'inactive' WHERE id = 12;
Show answer
The database rejects the update because the resulting row would no longer satisfy the view condition and would disappear from active_employees.