Introduction
Updatable views let you change data through a saved query, making it easier to work with complex data without touching the original tables directly.
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
CREATE VIEW active_customers AS SELECT id, name, email FROM customers WHERE status = 'active';
UPDATE active_customers SET email = 'new@example.com' WHERE id = 1;
INSERT INTO active_customers (id, name, email) VALUES (5, 'Anna', 'anna@example.com');
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), department VARCHAR(50), salary INT ); INSERT INTO employees VALUES (1, 'John', 'Sales', 50000), (2, 'Jane', 'HR', 55000), (3, 'Mike', 'Sales', 52000); CREATE VIEW sales_employees AS SELECT id, name, salary FROM employees WHERE department = 'Sales'; -- Update salary through the view UPDATE sales_employees SET salary = 53000 WHERE id = 3; -- Select to see the change SELECT * FROM employees WHERE id = 3;
updatable view in MySQL?CREATE VIEW view_name AS SELECT ... to create views.employees(id, name, salary) and the view:CREATE VIEW emp_view AS SELECT id, name FROM employees;id and name, excluding salary.name through the view is allowed because it maps directly to the table column.salary, assuming NOT NULL no default).salary fails because salary not in view.salary fails because salary not in view.CREATE VIEW v AS SELECT id, name FROM employees WHERE salary > 3000;UPDATE v SET salary = 5000 WHERE id = 5; fail?id and name; salary is excluded.orders table that shows only order_id and customer_id. You also want to allow updates to customer_id through the view. Which of the following is the best approach?customer_id to allow updates on it.customer_id (as in A) prevents updating it.Pass the test and this pattern moves unknown