Concept Flow - Updatable views and limitations
Create View
Query View
Check if View is Updatable
Reflect Changes in View
This flow shows how an update on a view is checked for updatability and either applied to the base table or rejected.
Jump into concepts and practice - no test required
CREATE VIEW v_emp AS SELECT emp_id, emp_name FROM employees; UPDATE v_emp SET emp_name = 'Anna' WHERE emp_id = 2;
| Step | Action | Check/Condition | Result | Effect |
|---|---|---|---|---|
| 1 | Create view v_emp | View definition simple select from one table | View created | View ready for queries |
| 2 | Update v_emp set emp_name='Anna' where emp_id=2 | Is view updatable? (simple select, no joins, no aggregates) | Yes | Proceed to update base table |
| 3 | Apply update to employees table | Row with emp_id=2 exists? | Yes | emp_name changed to 'Anna' in employees |
| 4 | Reflect update in view | View reads updated base table | Updated row visible in view | View shows emp_name='Anna' for emp_id=2 |
| 5 | Try update on non-updatable view | View has join or aggregate | No | Update rejected with error |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| emp_name for emp_id=2 | 'John' | Attempt to update to 'Anna' | 'Anna' | 'Anna' | 'Anna' |
Updatable views allow changes through the view to affect base tables. Views are updatable if based on a single table without joins, aggregates, or distinct. Updates on non-updatable views cause errors. Check view definition before updating. Changes reflect in base tables and then in the view.
employees showing id and name?CREATE VIEW dept_salary AS SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id;
avg_salary through this view?CREATE VIEW emp_dept AS SELECT e.id, e.name, d.name AS dept_name FROM employees e JOIN departments d ON e.dept_id = d.id;
dept_name through this view causes an error. What is the main reason?