Consider a table Employees with columns id, name, and salary. A view EmpView is created as SELECT id, name, salary FROM Employees. What will be the result of this query after executing UPDATE EmpView SET salary = salary + 1000 WHERE id = 2;?
Assume the original Employees table data is:
id | name | salary ---+-------+-------- 1 | Alice | 5000 2 | Bob | 6000 3 | Carol | 5500
What will be the salary of Bob after the update?
CREATE TABLE Employees (id INT PRIMARY KEY, name VARCHAR(50), salary INT); INSERT INTO Employees VALUES (1, 'Alice', 5000), (2, 'Bob', 6000), (3, 'Carol', 5500); CREATE VIEW EmpView AS SELECT id, name, salary FROM Employees; UPDATE EmpView SET salary = salary + 1000 WHERE id = 2; SELECT salary FROM Employees WHERE id = 2;
Simple views that directly select from one table without aggregation are usually updatable.
The view EmpView is a simple select from Employees. Updating the view updates the underlying table. Bob's salary increases from 6000 to 7000.
Which of the following statements correctly describes a limitation when trying to update a view that is created by joining two tables?
Think about how the database knows which table to update when multiple tables are joined.
Views created by joining tables are generally not fully updatable. Some DBMS allow updates on columns from one base table only, not both at the same time.
Given the following view definitions, which one is NOT updatable according to standard SQL rules?
Consider if the view contains computed columns or expressions.
View V2 contains a computed column salary * 1.1. Views with computed columns are not updatable because the database cannot map updates back to the base table column.
Consider the view and update statement below:
CREATE VIEW DeptEmp AS SELECT e.id, e.name, d.department_name FROM Employees e JOIN Departments d ON e.department_id = d.id; UPDATE DeptEmp SET department_name = 'Sales' WHERE id = 3;
The update fails with an error. Why?
Think about how updates work on joined views.
Views with joins are generally not updatable for columns from the joined table unless INSTEAD OF triggers or rules are defined. The DBMS cannot decide how to update the department_name column in Departments table through this view.
Which of the following conditions will prevent a view from being updatable in most SQL database systems?
Think about what aggregate functions do to rows.
Views with aggregate functions summarize data and do not represent individual rows in the base table, so they cannot be updated directly.