SQL DROP VIEW
Removes a saved query. Nothing that reads the base tables is touched; the only casualties are things that read the view itself, and engines disagree about how loudly they complain.
- Категория
- Представления
- In this category
- 3 of 3
Синтаксис
DROP VIEW [IF EXISTS] view_name [, ...]
DROP VIEW view_name [RESTRICT | CASCADE] -- PostgreSQL
Each block is a separate form. The support table and the examples show which engine takes which.
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Да | IF EXISTS from 2016; several views comma-separated |
| PostgreSQL | Да | RESTRICT by default; CASCADE removes dependent views |
| MySQL | Да | several views per statement; IF EXISTS logs a note instead of failing |
| SQLite | Да | IF EXISTS; one view per statement |
| Oracle | Да | IF EXISTS from 23c |
Примеры
Any engine
DROP VIEW IF EXISTS open_orders;
PostgreSQL
DROP VIEW open_orders CASCADE; -- dependent views go too
MySQL
DROP VIEW IF EXISTS v_tmp1, v_tmp2;
SQL Server
DROP VIEW IF EXISTS dbo.open_orders;
Полезно знать
- PostgreSQL is the one that checks dependencies by default: RESTRICT (the default) fails the drop when another view depends on it, and CASCADE removes the dependents too. SQL Server, MySQL, SQLite and Oracle let the drop through and leave dependent views broken until someone recreates what they needed; SQL Server refuses the drop only when a dependent view was created WITH SCHEMABINDING.
- Oracle's recycle bin holds dropped tables, not views, so a dropped view is gone unless you kept the definition scripted somewhere.
- MySQL's DROP VIEW IF EXISTS v1, v2, v3 reports a warning for each missing name instead of failing, which makes it a tidy closer for migration scripts.
- Dropping a view never drops its base tables; the risk runs the other way (drop the table and the view breaks). Keep view definitions in source control, since no engine offers an undo once the drop is committed (PostgreSQL, SQL Server and SQLite can still roll it back inside an open transaction).
Questions about DROP VIEW
Does DROP VIEW delete any data?
No. A view stores a query, not rows, so dropping it removes only the definition. The base tables and their data stay exactly as they were.
How do I drop a view only if it exists?
DROP VIEW IF EXISTS works on SQL Server 2016+, PostgreSQL, MySQL, SQLite and Oracle 23c+. Older engines need a catalog check first or a caught error.
What happens to views built on a dropped view?
On PostgreSQL the drop fails unless you add CASCADE, which removes the dependents deliberately. On SQL Server, MySQL, SQLite and Oracle the drop succeeds, and the dependent views fail with an error the next time they are used (SQL Server: invalid object name; MySQL: error 1356, invalid table or column). The exception is a SQL Server view created WITH SCHEMABINDING, which blocks the drop outright.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.