Update Employee Data Through a Simple View
Update Data through a Simple Updatable View
Write a PostgreSQL query to update the salary of an employee through an updatable view.
Solution:
-- Increase the salary by 10% for the employee with ID 101 using the view.
UPDATE EmployeeView -- Specify the updatable view based on the Employees table.
SET salary = salary * 1.10 -- Increase salary by 10%.
WHERE employee_id = 101; -- Identify the employee.
Explanation:
- Purpose of the Query:
- The goal is to update underlying table data via a simple view that directly maps to one table.
- This demonstrates that simple views (without joins or aggregates) are inherently updatable.
- Key Components:
- UPDATE EmployeeView : Specifies the view to update.
- SET salary = salary * 1.10 : Modifies the salary column.
- WHERE employee_id = 101 : Targets a specific employee.
- Real-World Application:
- Enables application developers to expose only needed columns and allow updates without direct table access.
Notes:
- The view must be defined simply (e.g., a single table without complex expressions) to be updatable by default.
For more Practice: Solve these Related Problems:
- Write a PostgreSQL query to update the bonus amount for an employee via a simple updatable view that directly maps to the Employees table.
- Write a PostgreSQL query to update both the salary and department fields through a simple updatable view, ensuring the update affects only records with a non-null department.
- Write a PostgreSQL query to update the hire date of an employee using a simple updatable view while recalculating a derived column from the base table.
- Write a PostgreSQL query to update an employee’s performance score via a simple updatable view, where the new value is computed based on a subquery.
Go to:
PREV : PostgreSQL Updating views Home.
NEXT : Update Data through a View with WITH CHECK OPTION.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
