Updatable Views

Updating Through a View

Simple views (one table, no GROUP BY, DISTINCT, aggregates or UNION) can be updated in MySQL, PostgreSQL, SQL Server and Oracle. The change goes to the base table.

CREATE VIEW IndianCustomers AS
SELECT CustomerID, CustomerName, City FROM Customers WHERE Country = 'India';

UPDATE IndianCustomers SET City = 'Pune' WHERE CustomerID = 4;   -- updates Customers

WITH CHECK OPTION

WITH CHECK OPTION rejects changes through the view that would make the row fall outside the view.

CREATE VIEW IndianCustomers AS
SELECT * FROM Customers WHERE Country = 'India'
WITH CHECK OPTION;

UPDATE IndianCustomers SET Country = 'UK' WHERE CustomerID = 4;
-- Error: the row would no longer be visible through the view

Note

Note: SQLite views are read-only. To write through them, you need an INSTEAD OF trigger.