Updatable views and limitations in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we update data through a view, the database must translate that update to the underlying tables.
We want to understand how the time to perform updates grows as the data size increases.
Analyze the time complexity of updating a simple updatable view.
CREATE VIEW EmployeeView AS
SELECT EmployeeID, Name, DepartmentID
FROM Employees
WHERE DepartmentID = 10;
UPDATE EmployeeView
SET Name = 'Alex'
WHERE EmployeeID = 5;
This code updates the name of an employee in a view filtered by department.
Look for repeated work the database does when processing the update.
- Primary operation: Searching the underlying Employees table for the matching EmployeeID.
- How many times: Once per update statement, but the search may scan rows depending on indexing.
The time to find the employee depends on how the table is searched.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 (if no index, scan all rows) |
| 100 | 100 (full scan) or fewer if indexed |
| 1000 | 1000 (full scan) or fewer if indexed |
Pattern observation: Without an index, the search grows linearly with table size; with an index, it stays fast.
Time Complexity: O(n)
This means the update time grows roughly in proportion to the number of rows in the underlying table when no index is used.
[X] Wrong: "Updating a view always takes constant time regardless of table size."
[OK] Correct: The database must find the matching rows in the base table, which can take longer as the table grows if no index helps.
Understanding how updates through views translate to base tables helps you explain database behavior clearly and shows you grasp practical data handling.
"What if the view joins multiple tables? How would that affect the time complexity of updates?"
Practice
Solution
Step 1: Understand what an updatable view is
An updatable view lets you change data (insert, update, delete) through the view as if it were a table.Step 2: Compare options to definition
Options B, C, and D describe views that are either read-only or complex and usually not updatable.Final Answer:
A view that allows you to insert, update, or delete rows through it. -> Option DQuick Check:
Updatable view = allows data changes [OK]
- Thinking all views allow data changes
- Confusing aggregate views as updatable
- Assuming joins always allow updates
employees showing id and name?Solution
Step 1: Identify simple view syntax
CREATE VIEW emp_view AS SELECT id, name FROM employees; selects columns directly from one table without grouping or joins, which is valid for an updatable view.Step 2: Check other options for complexity
Options A and C use GROUP BY or aggregate functions, making views non-updatable. CREATE VIEW emp_view AS SELECT id, name FROM employees JOIN departments ON employees.dept_id = departments.id; uses JOIN, also usually non-updatable.Final Answer:
CREATE VIEW emp_view AS SELECT id, name FROM employees; -> Option CQuick Check:
Simple select from one table = updatable view syntax [OK]
- Adding GROUP BY or aggregates in view
- Using JOINs in view definition
- Forgetting to select from only one table
CREATE VIEW dept_salary AS SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id;
Which of the following statements is true when trying to update
avg_salary through this view?Solution
Step 1: Analyze the view's use of aggregation
The view uses AVG() and GROUP BY, which creates a summary, not individual rows.Step 2: Understand update limitations on aggregated views
Aggregated columns like avg_salary cannot be updated directly because they do not map to single rows.Final Answer:
You cannot update avg_salary because the view uses aggregation. -> Option BQuick Check:
Aggregated views are not updatable by default [OK]
- Trying to update aggregate columns
- Assuming updates affect underlying rows automatically
- Ignoring need for triggers on complex views
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;
Attempting to update
dept_name through this view causes an error. What is the main reason?Solution
Step 1: Identify the view's structure
The view uses a JOIN between employees and departments tables.Step 2: Recall update limitations on joined views
Views involving JOINs are generally not updatable because the database cannot determine which table to update.Final Answer:
Views with JOINs are not updatable by default. -> Option AQuick Check:
JOIN views block updates unless special rules apply [OK]
- Thinking alias columns block updates
- Believing WHERE clause affects updatability
- Assuming all columns must be selected
Solution
Step 1: Understand the requirement
You want to update employee data but also show department names, which requires joining tables.Step 2: Recognize limitations of joined views
Joined views are not updatable by default, but INSTEAD OF triggers can enable updates by defining custom behavior.Step 3: Evaluate other options
Create a simple view on employees only and join departments in queries outside the view. avoids joins in the view but does not show department names in the view itself. Options C and D use aggregates, which block updates.Final Answer:
Create a view joining employees and departments, then use INSTEAD OF triggers to handle updates. -> Option AQuick Check:
Use triggers to update joined views [OK]
- Trying to update aggregates directly
- Ignoring triggers for complex views
- Using simple views that lack needed columns
