0
0
SQLquery~10 mins

Updatable views and limitations in SQL - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
SQL
CREATE VIEW v_emp AS
SELECT emp_id, emp_name FROM employees;

UPDATE v_emp SET emp_name = 'Anna' WHERE emp_id = 2;
This code creates a simple view and updates a column through the view if it is updatable.
Execution Table
StepActionCheck/ConditionResultEffect
1Create view v_empView definition simple select from one tableView createdView ready for queries
2Update v_emp set emp_name='Anna' where emp_id=2Is view updatable? (simple select, no joins, no aggregates)YesProceed to update base table
3Apply update to employees tableRow with emp_id=2 exists?Yesemp_name changed to 'Anna' in employees
4Reflect update in viewView reads updated base tableUpdated row visible in viewView shows emp_name='Anna' for emp_id=2
5Try update on non-updatable viewView has join or aggregateNoUpdate rejected with error
💡 Update stops if view is not updatable or after base table is updated successfully
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
emp_name for emp_id=2'John'Attempt to update to 'Anna''Anna''Anna''Anna'
Key Moments - 3 Insights
Why can't we update a view that has a join?
Because the view combines multiple tables, the database cannot determine which base table to update, so the update is rejected as shown in execution_table row 5.
What makes a view updatable?
A view is updatable if it is based on a single table without aggregates, group by, or distinct, as checked in execution_table row 2.
Does updating a view always change the data?
No, only if the view is updatable and the base table row exists, otherwise the update is rejected or has no effect (execution_table rows 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the database decide if the view is updatable?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Check the 'Check/Condition' column in execution_table row 2 for the updatability check.
According to variable_tracker, what is the value of emp_name for emp_id=2 after step 3?
A'Anna'
B'Anna' updated in view only
C'John'
DNo change
💡 Hint
Look at variable_tracker row for emp_name after Step 3.
If the view included a join, what would happen when trying to update it?
AUpdate succeeds and changes both tables
BUpdate is rejected with error
CUpdate succeeds but changes only one table
DUpdate silently ignored
💡 Hint
See execution_table row 5 where update on non-updatable view is rejected.
Concept Snapshot
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.
Full Transcript
This visual execution trace shows how SQL handles updates on views. First, a view is created from a single table. When an update is issued on the view, the database checks if the view is updatable, meaning it is a simple select from one table without joins or aggregates. If yes, the update is applied to the base table row matching the condition. The change then appears in the view. If the view is not updatable, such as when it includes joins, the update is rejected with an error. Variables like the employee name change step-by-step as the update proceeds. Key moments clarify why joins prevent updates and what makes a view updatable. Quiz questions test understanding of these steps and outcomes.