Updatable and read-only views
Overview
An updatable view accepts a write and translates it into changes to its underlying relation. In PostgreSQL 17 a simple single-relation view without grouping, distinct or other disqualifying constructs may be automatically updatable; computed columns may be read-only. SQLite ordinary views reject writes unless a matching INSTEAD OF trigger implements them. The fixture includes a teaching view with an update trigger.
First example
BEGIN;UPDATE vw_edit SET price=130 WHERE id=1;SELECT id,price FROM vw_product WHERE id=1;ROLLBACK;Observed SQLite result (rows in JSON):
[ [ 1, 130 ]]How it works
The first example updates a price through vw_edit; the INSTEAD OF UPDATE OF price trigger uses OLD.id and NEW.price to update exactly the corresponding base row. The second changes two prices through the same interface and checks the aggregate. Neither trigger implements inserts, deletes or arbitrary column changes. PostgreSQL automatic updates are not the same mechanism. Aggregate or join-based views generally need special handling rather than assuming all writes have one unambiguous destination. Verify base-table constraints and inspect base rows after trigger-driven updates; SQLite changes() does not count the underlying INSTEAD OF trigger work as direct view changes.
Practical example
BEGIN;UPDATE vw_edit SET price=price+10 WHERE id IN (1,2);SELECT SUM(price) FROM vw_active;ROLLBACK;Observed SQLite result (rows in JSON):
[ [ 320 ]]Practice and mistakes
Practice: increase the price of product 2 through vw_edit by 20, then inspect its base row. Hint 1: use SET price=price+20. Hint 2: use a transaction and rollback. Expect 220. Mistake: attempting DELETE through a view whose trigger implements only price updates.