Updatable views in MySQL - Time & Space Complexity
When we update data through a view in MySQL, it is important to understand how the database processes these updates.
We want to know how the time to update grows as the data behind the view grows.
Analyze the time complexity of updating data through an updatable view.
CREATE VIEW employee_view AS
SELECT id, name, salary
FROM employees
WHERE department = 'Sales';
UPDATE employee_view
SET salary = salary * 1.1
WHERE id = 101;
This code creates a view showing sales employees and then updates the salary of one employee through the view.
Look for repeated steps when the update runs.
- Primary operation: Searching the base table for the matching row to update.
- How many times: The update targets one row, so the search depends on how the database finds that row.
As the number of employees grows, the time to find the row may change.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks if no index |
| 100 | About 100 checks if no index |
| 1000 | About 1000 checks if no index |
Pattern observation: Without an index, the search grows linearly with the number of rows.
Time Complexity: O(n)
This means the time to update through the view grows roughly in direct proportion to the number of rows in the base table.
[X] Wrong: "Updating through a view is always faster because it looks simpler."
[OK] Correct: The view is just a window to the base table; the database still needs to find and update the actual data, which can take time depending on the table size.
Understanding how updates through views work helps you explain database behavior clearly and shows you know how data access affects performance.
"What if the base table had an index on the 'id' column? How would that change the time complexity of the update through the view?"
Practice
updatable view in MySQL?Solution
Step 1: Understand what a view is
A view is a saved query that shows data from one or more tables.Step 2: Define updatable view
An updatable view lets you change data in the underlying table through the view itself.Final Answer:
A view that allows you to change the data in the underlying table through it. -> Option BQuick Check:
Updatable view = allows data change [OK]
- Thinking views store data separately
- Confusing read-only views with updatable views
- Assuming all views are updatable
Solution
Step 1: Recall MySQL view creation syntax
MySQL usesCREATE VIEW view_name AS SELECT ...to create views.Step 2: Understand updatable views syntax
There is no special keyword for updatable views; they are updatable if the SELECT is simple enough.Final Answer:
CREATE VIEW view_name AS SELECT * FROM table_name; -> Option DQuick Check:
Standard CREATE VIEW syntax = CREATE VIEW view_name AS SELECT * FROM table_name; [OK]
- Adding non-existent keywords like UPDATABLE or WITH UPDATE
- Using wrong syntax keywords
- Assuming updatable views need special creation syntax
employees(id, name, salary) and the view:CREATE VIEW emp_view AS SELECT id, name FROM employees;Which of the following statements will succeed?
Solution
Step 1: Check view columns and underlying table
The view selects onlyidandname, excludingsalary.Step 2: Determine which operations are allowed
Updatingnamethrough the view is allowed because it maps directly to the table column.
INSERT fails (missingsalary, assuming NOT NULL no default).
DELETE referencingsalaryfails becausesalarynot in view.
Updatingsalaryfails becausesalarynot in view.Final Answer:
UPDATE emp_view SET name = 'Alice' WHERE id = 1; -> Option AQuick Check:
Update on view columns allowed = UPDATE emp_view SET name = 'Alice' WHERE id = 1; [OK]
- Trying to update columns not in the view
- Assuming inserts/deletes always work on views
- Ignoring missing columns in the view
CREATE VIEW v AS SELECT id, name FROM employees WHERE salary > 3000;Why does the statement
UPDATE v SET salary = 5000 WHERE id = 5; fail?Solution
Step 1: Check the columns in the view and the UPDATE target
The view selects onlyidandname;salaryis excluded.Step 2: Check other options
A WHERE clause does not prevent updates to included columns.
Views can have multiple columns and be updatable.
The view name is valid.Final Answer:
Because the view does not include the salary column. -> Option AQuick Check:
Cannot update column not in view [OK]
- Thinking WHERE clause blocks updates
- Assuming multiple columns prevent updates
- Ignoring missing columns in the view
orders table that shows only order_id and customer_id. You also want to allow updates to customer_id through the view. Which of the following is the best approach?Solution
Step 1: Identify columns needed in the view
The view must includecustomer_idto allow updates on it.Step 2: Avoid issues that limit updatability or scope
Adding a WHERE clause (as in B) limits updates to only recent orders.
Including extra columns (as in C) shows more than requested.
Excludingcustomer_id(as in A) prevents updating it.
D provides exactly the requested columns without restrictions.Final Answer:
CREATE VIEW order_view AS SELECT order_id, customer_id FROM orders; Then update customer_id through order_view. -> Option CQuick Check:
Simple view with needed columns = updatable [OK]
- Adding WHERE filters that limit updates
- Excluding columns you want to update
- Adding unnecessary columns that confuse
Ready to prove it?
Pass the test and this pattern moves unknown
