Updatable Views & WITH CHECK OPTION
Update through views safely and enforce WITH CHECK OPTION constraints.
In plain words
You can change data through a simple view, and CHECK OPTION makes sure the changed rows still belong in that view.
Think of it like…
A department's shared folder that only shows files tagged with that department: CHECK OPTION means you can't save a file into the folder with another department's tag, and you can't retag a file so it disappears from the folder.
Where you’ve seen it
A support tool writes through CREATE VIEW eu_customers AS SELECT id, name, email, region FROM customers WHERE region = 'EU' WITH CHECK OPTION; When an agent tries UPDATE eu_customers SET region = 'US' WHERE id = 7, PostgreSQL rejects it with "new row violates check option for view", so EU staff can't accidentally move customers out of their own scope.
Words you’ll hear
- Updatable view
- A view simple enough that writes map to rows of one base table.
- WITH CHECK OPTION
- Rejects inserted or updated rows that wouldn't be visible through the view.
- LOCAL vs CASCADED
- Check only this view's conditions, or also those of the views it's built on.
- INSTEAD OF trigger
- Custom logic that makes a complex (non-updatable) view writable.
Visual diagram
Theory & example queries
Which views can be written to
In PostgreSQL 9.3+, a view is automatically updatable when it selects from exactly one table or updatable view, with no WITH, DISTINCT, GROUP BY, HAVING, LIMIT, OFFSET or set operations (UNION, INTERSECT, EXCEPT) at the top level, and no aggregates, window functions or set-returning functions in the select list. Columns that are plain references to base columns can be updated; computed columns such as price * 1.18 AS price_with_tax are read-only.
MySQL and SQL Server follow similar rules: each written row must map to exactly one base-table row, and a statement may change columns of only one base table. Inserting through a view fills columns the view doesn't expose with their defaults, so those columns need defaults or must be nullable. For views with joins or aggregates, write an INSTEAD OF trigger that defines what an insert or update should do.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
CREATE VIEW customer_order_totals ASSELECT customer_id, SUM(amount) AS totalFROM ordersGROUP BY customer_id;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
UPDATE customer_order_totalsSET total = 0PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Expected view update error
Note: PostgreSQL-style diagnostic; other databases may use different wording for the same non-updatable aggregate view.
Running
fails, because a total doesn't map to one row of orders.
Key takeaway: One table, no aggregation or set operations, plain columns: updatable.
CHECK OPTION: LOCAL and CASCADED
Without CHECK OPTION, INSERT INTO eu_customers (name, email, region) VALUES ('Ana', 'ana@example.com', 'US') succeeds and the new row is immediately invisible through the view. With CHECK OPTION, the database tests each new or updated row against the view's WHERE clause and rejects violations; DELETE is never checked, since a deleted row isn't written anywhere.
For views built on views, WITH CASCADED CHECK OPTION (the default when you just write WITH CHECK OPTION) checks this view's conditions and those of every view underneath; WITH LOCAL CHECK OPTION checks only conditions defined in this view, plus any underlying views that have their own check option. CHECK OPTION is about data scope, not permissions: combine it with GRANTs on the view and no direct access to the base table.
| id | name | city | |
|---|---|---|---|
| 1 | Maya | maya@example.com | Pune |
| 2 | Ravi | ravi@example.com | Delhi |
| 3 | Noor | noor@example.com | Mumbai |
CREATE VIEW active_customers ASSELECT *FROM customersWHERE status = 'active';CREATE VIEW active_eu ASSELECT *FROM active_customersWHERE region = 'EU'WITH CASCADED CHECK OPTION;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Expected CHECK OPTION failure
Note: Assumes the inserted row has region EU but status pending. CASCADED CHECK OPTION checks active_eu and the underlying active_customers predicate.
Inserting a pending EU customer through active_eu fails, because CASCADED also checks status = 'active'.
Key takeaway: CHECK OPTION enforces the WHERE on writes; CASCADED includes underlying views.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Keep the view simple
Select from one table with a WHERE clause and plain column references so it's automatically updatable.
SELECT id, name, email, regionFROM customersWHERE region = 'EU'When to use it & trade-offs
What it is
A simple view over one table is updatable: INSERT, UPDATE and DELETE on the view are passed through to the base table. Without protection, you can update a row so it no longer matches the view's WHERE clause, making it vanish from the view, or insert rows the view can't show. WITH CHECK OPTION stops that: any row written through the view must satisfy the view's conditions, or the statement fails.
Why it matters: Views are often used to give a team or service a restricted slice of a table, such as active customers only or one region's orders. If that slice is writable, CHECK OPTION is what turns the view's WHERE clause from a filter into a rule, so a regional team can't move orders to another region by mistake. Knowing which views are updatable also explains confusing "cannot update view" errors.
When to reach for it
- Giving a team write access to their region or status only
- Keeping legacy column names via a view during a migration
- Hiding sensitive columns while allowing edits to others
When to avoid or be careful
- Writable restricted views without CHECK OPTION
- Expecting views with GROUP BY or DISTINCT to be updatable
- Granting access to both the view and its base table
Ways to write through a restricted slice
Strengths: No code; the database enforces the scope.
Watch out: Only works for simple, single-table views.
Strengths: Any logic you need.
Watch out: Trigger code to write, test and maintain.
Strengths: Applies to every query path.
Watch out: More complex to set up and test.
Interview signals & numbers to know
When this topic comes up
- “Let the EU team edit only EU customers”
- "cannot update view" errors
- “Rows disappear after updating through a view”
- Views for access control
- INSTEAD OF triggers
Benchmarks & rules of thumb
- 1
- base table for an automatically updatable view
- CASCADED
- the default CHECK OPTION mode
- PostgreSQL 9.3
- automatically updatable views
Common mistakes & follow-ups
Common interview mistakes
- Creating a filtered view for a team without CHECK OPTION, so their inserts for other regions succeed and vanish.
- Adding GROUP BY to a view the application writes to, breaking every update.
- Inserting through a view that hides a NOT NULL column with no default.
Likely interviewer follow-ups
- What makes a view updatable?
- What does WITH CHECK OPTION prevent?
- How would you make a view over a join writable?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Updatable Views & WITH CHECK OPTION with hands-on SQL practice and interview Q&A.