← Back to SQL, page 12

sql-0239 · Medium mid

Can you UPDATE or INSERT through a view?

In PostgreSQL, a simple view over one table, with no aggregates, DISTINCT or joins, is automatically updatable. SQLite views are read-only unless you add INSTEAD OF triggers that turn writes into changes on the base tables. WITH CHECK OPTION in PostgreSQL rejects writes that would make the row disappear from the view.

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SQLite rejects a write to a view:

sql
CREATE VIEW eng AS SELECT id, name, salary FROM employees WHERE dept_id = 1;
UPDATE eng SET salary = salary + 1000 WHERE id = 2;
-- error on purpose
text
Error: cannot modify eng because it is a view

PostgreSQL updates the base table through a simple view:

sql
CREATE VIEW eng AS SELECT id, name, salary, dept_id FROM employees WHERE dept_id = 1;
UPDATE eng SET salary = salary + 1000 WHERE id = 2;
SELECT name, salary FROM employees WHERE id = 2;
namesalary
Ravi96000

WITH CHECK OPTION stops changes that would move a row outside the view's filter:

sql
CREATE VIEW eng_checked AS SELECT id, name, dept_id FROM employees WHERE dept_id = 1 WITH CHECK OPTION;
UPDATE eng_checked SET dept_id = 2 WHERE id = 3;
-- error on purpose
text
Error: new row violates check option for view "eng_checked"

In SQLite, an INSTEAD OF trigger makes the view writable by defining what a write means:

sql
CREATE VIEW eng2 AS SELECT id, name, salary FROM employees WHERE dept_id = 1;
CREATE TRIGGER eng2_update INSTEAD OF UPDATE OF salary ON eng2
BEGIN
  UPDATE employees SET salary = NEW.salary WHERE id = OLD.id;
END;
UPDATE eng2 SET salary = salary + 1000 WHERE id = 2;
SELECT name, salary FROM employees WHERE id = 2;
namesalary
Ravi96000
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

-- error on purpose
CREATE VIEW eng AS SELECT id, name, salary FROM employees WHERE dept_id = 1;
UPDATE eng SET salary = salary + 1000 WHERE id = 2;

CREATE VIEW eng2 AS SELECT id, name, salary FROM employees WHERE dept_id = 1;
CREATE TRIGGER eng2_update INSTEAD OF UPDATE OF salary ON eng2
BEGIN
  UPDATE employees SET salary = NEW.salary WHERE id = OLD.id;
END;
UPDATE eng2 SET salary = salary + 1000 WHERE id = 2;
SELECT name, salary FROM employees WHERE id = 2;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • PostgreSQL simple views are automatically updatable
  • SQLite views are read-only without INSTEAD OF triggers
  • WITH CHECK OPTION keeps writes inside the view's filter
  • Views with joins or aggregates need triggers to be writable

Common mistakes

  • Assuming every view can be updated
  • Letting updates move rows out of a filtered view silently

Follow-ups

  • How would you make a join view writable?
  • What is LOCAL versus CASCADED CHECK OPTION?

Tips

  • Contrast the two engines