MySQL + SQL · Lesson 94

Updatable Views in MySQL

Can a View Be Updated?

Some views are updatable — an INSERT/UPDATE on the view changes the base table. This works only for simple views (one table, no GROUP BY, no aggregate, no DISTINCT).

Example

CREATE VIEW active_students AS
SELECT roll_no, name, status FROM students WHERE status = 'active';

UPDATE active_students SET name = 'Aman K' WHERE roll_no = 1;
-- this updates the real students table

When a View is NOT Updatable

  • Uses aggregate functions (SUM, COUNT...).
  • Has GROUP BY or DISTINCT.
  • Joins multiple tables in a complex way.

Summary

  • Simple single-table views can be updated, changing the base table.
  • Views with aggregates, GROUP BY or DISTINCT are read-only.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

WhatsApp