Introduction
Updatable views let you change data through a saved query, making it easier to work with complex data without changing the original tables directly.
Jump into concepts and practice - no test required
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
CREATE VIEW simple_view AS SELECT id, name, age FROM employees WHERE active = 1;
CREATE VIEW joined_view AS SELECT e.id, e.name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.id;
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), age INT, active BOOLEAN ); INSERT INTO employees VALUES (1, 'Alice', 30, TRUE), (2, 'Bob', 25, FALSE); CREATE VIEW active_employees AS SELECT id, name, age FROM employees WHERE active = TRUE; -- Update through the view UPDATE active_employees SET age = 31 WHERE id = 1; -- Check the update SELECT * FROM employees WHERE id = 1;
employees showing id and name?CREATE VIEW dept_salary AS SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id;
avg_salary through this view?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;
dept_name through this view causes an error. What is the main reason?