0
0
MySQLquery~20 mins

Updatable views in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Updatable Views Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of updating a simple updatable view
Given a table Employees with columns 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;
AThe salary of employee with id 3 remains 60000; no change occurs.
BThe update fails because views cannot be updated.
CThe update changes salary in HighEarners view only, Employees table remains unchanged.
DThe salary of employee with id 3 becomes 65000 in Employees and HighEarners view.
Attempts:
2 left
💡 Hint
Think about whether the view is updatable and if changes reflect in the base table.
🧠 Conceptual
intermediate
1:30remaining
Which views are not updatable?
Which of the following types of views in MySQL are generally not updatable?
AViews that include only columns from one table with no WHERE clause.
BViews that select directly from a single table without joins.
CViews that use aggregate functions like SUM or COUNT.
DViews that select all columns from a single table.
Attempts:
2 left
💡 Hint
Think about what happens when a view summarizes data.
📝 Syntax
advanced
2: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';
ACREATE VIEW ActiveUsers AS SELECT id, username FROM Users WHERE status = 'active' WITH CHECK OPTION;
BCREATE VIEW ActiveUsers AS SELECT id, username FROM Users WHERE status = 'active' WITH UPDATE OPTION;
CCREATE VIEW ActiveUsers AS SELECT id, username FROM Users WHERE status = 'active' WITH READ ONLY;
DCREATE VIEW ActiveUsers AS SELECT id, username FROM Users WHERE status = 'active';
Attempts:
2 left
💡 Hint
Check which clauses are valid in MySQL view definitions.
optimization
advanced
2:00remaining
Optimizing updates through views with joins
Consider a view defined as:
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?
AYou can update the employee's name through the view, but not the department name.
BYou cannot update any columns through this view because it uses a join.
CYou can update both employee name and department name through the view.
DYou can update the department name but not the employee name through the view.
Attempts:
2 left
💡 Hint
Think about which table columns the view maps to and MySQL's rules for updatable views with joins.
🔧 Debug
expert
2:30remaining
Why does this update on a view fail?
Given the view:
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?
AThe update violates the view's WHERE condition, so MySQL rejects it.
BViews with WHERE clauses are never updatable in MySQL.
CThe order_date column is read-only in all views.
DThe order_id column is not a primary key, so update fails.
Attempts:
2 left
💡 Hint
Consider what happens if the update changes a row so it no longer meets the view's filter.