0
0
MySQLquery~10 mins

Updatable views in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Updatable views
Create View
Query View
Update View Data?
Update Base Table
Reflect Changes in View
This flow shows how an update on a view is checked and either applied to the base table or rejected if not allowed.
Execution Sample
MySQL
CREATE VIEW v_emp AS SELECT id, name, salary FROM employees;
UPDATE v_emp SET salary = salary + 100 WHERE id = 2;
This code creates a simple view and updates the salary of employee with id 2 through the view.
Execution Table
StepActionEvaluationResult
1Create view v_empView created on employees(id, name, salary)View v_emp ready
2Update v_emp set salary = salary + 100 where id = 2Check if view is updatableView is updatable
3Translate update to base tableUpdate employees set salary = salary + 100 where id = 2Base table updated
4Reflect changes in viewQuery view for id=2Salary increased by 100
5EndNo more updatesUpdate complete
💡 Update stops after applying changes to base table and reflecting in view
Variable Tracker
VariableStartAfter Step 3Final
salary (id=2)500051005100
Key Moments - 2 Insights
Why can we update the view but not all views are updatable?
Only views that directly map to a single base table without complex joins or aggregations are updatable, as shown in step 2 where the system checks if the view is updatable.
What happens if the view is not updatable?
The update is rejected and does not affect the base table, unlike step 3 where the update is translated and applied.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the salary of employee with id=2 after step 3?
A5000
B5100
C5200
D4900
💡 Hint
Check the variable_tracker row for salary after step 3
At which step does the system confirm the view can be updated?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the evaluation column in the execution_table for step 2
If the view included a join, what would happen at step 2?
AUpdate partially applied
BUpdate proceeds normally
CUpdate is rejected
DView is dropped
💡 Hint
Refer to key_moments about updatable views and step 2 evaluation
Concept Snapshot
CREATE VIEW view_name AS SELECT columns FROM table;
Updates on simple views affect the base table directly.
Only views without joins, aggregates, or complex expressions are updatable.
Updating a view translates to updating the underlying table.
If not updatable, update commands on the view fail.
Full Transcript
Updatable views in MySQL allow you to change data through a view if the view is simple enough. The process starts by creating a view that selects columns from a base table. When you try to update the view, MySQL checks if the view is updatable. If yes, it translates the update to the base table and applies it. The changes then show up in the view. If the view is complex, like having joins or aggregates, the update is rejected. This ensures data integrity and clear mapping between views and tables.