Updating and Dropping Views in MySQL
Once a view is created in MySQL, you can modify it using CREATE OR REPLACE VIEW or delete it entirely using DROP VIEW. This allows flexibility in managing virtual tables without affecting the underlying base tables.
1. Updating a View
Use CREATE OR REPLACE VIEW to update an existing view without dropping it.
This is useful if you want to change the columns or conditions in a view while keeping the same view name.
Syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
CREATE OR REPLACE VIEW active_customers AS
SELECT id, name
FROM customers
WHERE status = 'active';
2. Dropping a View
Use DROP VIEW to permanently remove an existing view from the database.
Syntax:
DROP VIEW view_name;
Example:
DROP VIEW active_customers;