Sub topic 5
Replacing and Dropping SQL Views
Assume you know SELECT, joins, CREATE TABLE, and basic privileges. A view is a stored query. Replacing a view changes the query that future users run. Dropping a view removes that stored query, but it does not remove rows from its base tables.
Replacing a view safely
Use CREATE OR REPLACE VIEW when you want to change a view without first removing it. Existing permissions often remain in place, and applications can continue referring to the same view name.
CREATE OR REPLACE VIEW employee_directory AS
SELECT e.id,
e.name,
d.name AS department
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id
WHERE e.status = 'active';This definition changes employee_directory so that inactive employees are no longer returned. The underlying employees and departments tables are unchanged.
A replacement is usually safest when the new view keeps the same output columns and compatible data types. Some database systems reject changes to column names or types through CREATE OR REPLACE VIEW. If the interface must change, create a new view, migrate its callers, and then remove the old one.
| Operation | Effect on the base tables | Typical use |
|---|---|---|
| `CREATE OR REPLACE VIEW` | No change | Change a view's query |
| `DROP VIEW` | No change | Remove an unused view |
Dropping a view
Use DROP VIEW to remove the view definition:
DROP VIEW employee_directory;Many systems support IF EXISTS, which avoids an error when the view is already absent:
DROP VIEW IF EXISTS employee_directory;A view can have dependent objects. For example, active_employee_directory might query employee_directory. Dropping the first view can then fail because the second view would no longer work.
Some database systems support CASCADE:
DROP VIEW employee_directory CASCADE;CASCADE also removes objects that depend on the view. This can have a wide and difficult-to-reverse effect, so inspect dependencies first and prefer the default restrictive behavior when you are unsure.
Common errors
- Replacing a view and silently changing a column's meaning can break reports.
- Dropping a view does not drop its base tables, but
CASCADEmay remove dependent views. - Replacing a view may fail when its column layout changes.
- SQL syntax differs across database products. Check whether your system supports
CREATE OR REPLACE VIEW,IF EXISTS, andCASCADE. - A view definition can depend on tables, functions, or other views. Changing those objects can break the view later.
Before a production change, save the current definition, check dependent objects, test the replacement in a development database, and verify the columns returned by the new definition.
Exercise
You need to add WHERE e.status = 'active' to an existing view while preserving its name. Which statement should you try first, and what should you check before running it?
Show answer
Try CREATE OR REPLACE VIEW. Check the database dialect, preserve compatible output columns and types, and inspect objects that depend on the view.