Sub topic 7
Writing Data Through a View
Assume you know SELECT, JOIN, WHERE, CREATE TABLE, and basic privileges. This chapter focuses on when INSERT, UPDATE, and DELETE can work through a view.
What makes a view writable?
A regular view stores a query definition, not a separate copy of its rows. When you change a row through a writable view, the database translates the operation into a change to the underlying base table.
A view is usually updatable when its query maps each view row to one base-table row. Simple projections and filters often qualify:
CREATE VIEW active_employees AS
SELECT id, name, salary, status, department_id
FROM employees
WHERE status = 'active';
UPDATE active_employees
SET salary = salary * 1.05
WHERE id = 7;
DELETE FROM active_employees
WHERE id = 9;
INSERT INTO active_employees
(id, name, salary, status, department_id)
VALUES
(12, 'Mina Chen', 6200, 'active', 2);The UPDATE and DELETE statements affect rows in employees. The INSERT also adds a row to employees.
Database systems differ in their exact rules. A view is commonly not directly writable when its query contains GROUP BY, aggregate functions such as SUM, DISTINCT, set operators such as UNION, or joins that make the target row unclear. Expressions and computed columns can also prevent direct updates.
| View query feature | Typical effect on writes |
|---|---|
| Selected columns from one base table | Often writable |
| `WHERE` filter | Often writable |
| `JOIN` between tables | Depends on the database and operation |
| `GROUP BY`, `SUM`, or `DISTINCT` | Usually not directly writable |
The disappearing-row problem
A filter controls which rows the view returns. Without an additional constraint, a write can make a row stop matching that filter:
UPDATE active_employees
SET status = 'inactive'
WHERE id = 7;The row is still in employees, but it disappears from active_employees. This can surprise users because the command succeeded, yet a later query through the view cannot find the changed row.
A view can also hide required base-table columns. An INSERT may fail if the underlying table has a required column that the view does not expose and that column has no default value.
Enforcing the view condition
WITH CHECK OPTION tells the database to reject an insert or update when the resulting row would not satisfy the view's query condition:
CREATE VIEW active_employees_checked AS
SELECT id, name, salary, status, department_id
FROM employees
WHERE status = 'active'
WITH CHECK OPTION;
UPDATE active_employees_checked
SET status = 'inactive'
WHERE id = 7;The update fails because the resulting row would not be visible through the view. An insert with status = 'inactive' fails for the same reason. The exact error wording and supported check-option forms vary by database.
WITH CHECK OPTION does not make a complex view writable. It only adds a visibility rule to a view that already supports the requested operation.
Choosing a writable design
Use a writable view when the view represents a clear subset of one table, such as active employees or customer-facing profile fields. Use a stored procedure or explicit base-table statements when a change must update several tables, apply business rules, or record an audit event.
The database may also allow an INSTEAD OF trigger for a complex view. Such a trigger defines the statements to run instead of the rejected INSERT, UPDATE, or DELETE. This is database-specific and requires careful testing.
Exercise
Why can an UPDATE through a view succeed but make the row disappear from that view?
Show answer
Without WITH CHECK OPTION, the update can change the row so it no longer satisfies the view's WHERE condition.