Challenge - 5 Problems
Updatable Views Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of updating a simple updatable view
Given a table Employees with columns
Assume employee with
id, name, and salary, and a view HighEarners defined as SELECT id, name, salary FROM Employees WHERE salary > 50000, what will be the result of this update?UPDATE HighEarners SET salary = salary + 5000 WHERE id = 3;Assume employee with
id = 3 has a salary of 60000 before the update.MySQL
CREATE TABLE Employees (id INT PRIMARY KEY, name VARCHAR(50), salary INT); INSERT INTO Employees VALUES (1, 'Alice', 45000), (2, 'Bob', 52000), (3, 'Charlie', 60000); CREATE VIEW HighEarners AS SELECT id, name, salary FROM Employees WHERE salary > 50000;
Attempts:
2 left
💡 Hint
Think about whether the view is updatable and if changes reflect in the base table.
✗ Incorrect
Since the view selects directly from Employees without aggregation or joins, it is updatable. Updating the view updates the underlying Employees table. So, employee 3's salary increases by 5000.
🧠 Conceptual
intermediate1:30remaining
Which views are not updatable?
Which of the following types of views in MySQL are generally not updatable?
Attempts:
2 left
💡 Hint
Think about what happens when a view summarizes data.
✗ Incorrect
Views with aggregate functions summarize data and do not map directly to rows in the base table, so they cannot be updated.
📝 Syntax
advanced2:00remaining
Identify the syntax error in creating an updatable view
Which option contains a syntax error when creating an updatable view in MySQL?
MySQL
CREATE VIEW ActiveUsers AS SELECT id, username FROM Users WHERE status = 'active';
Attempts:
2 left
💡 Hint
Check which clauses are valid in MySQL view definitions.
✗ Incorrect
MySQL supports WITH CHECK OPTION but does not support WITH UPDATE OPTION. Using WITH UPDATE OPTION causes a syntax error.
❓ optimization
advanced2:00remaining
Optimizing updates through views with joins
Consider a view defined as:
Which statement about updating the
CREATE VIEW EmpDept AS SELECT e.id, e.name, d.dept_name FROM Employees e JOIN Departments d ON e.dept_id = d.id;Which statement about updating the
EmpDept view is true?Attempts:
2 left
💡 Hint
Think about which table columns the view maps to and MySQL's rules for updatable views with joins.
✗ Incorrect
MySQL allows updates through views with joins only if the update affects columns from one table and the join keys are preserved. Updating employee name is allowed; updating department name is not.
🔧 Debug
expert2:30remaining
Why does this update on a view fail?
Given the view:
Attempting to run:
causes an error. What is the most likely reason?
CREATE VIEW RecentOrders AS SELECT order_id, customer_id, order_date FROM Orders WHERE order_date >= '2024-01-01' WITH CHECK OPTION;Attempting to run:
UPDATE RecentOrders SET order_date = '2023-12-31' WHERE order_id = 101;causes an error. What is the most likely reason?
Attempts:
2 left
💡 Hint
Consider what happens if the update changes a row so it no longer meets the view's filter.
✗ Incorrect
MySQL rejects updates that would cause rows to no longer satisfy the view's WHERE clause when the view is created with WITH CHECK OPTION.