00:00

MySQL Drop View

Last updated:

Views pile up over time: one for a report that's no longer run, another from an old version of the app. DROP VIEW removes a view's definition. Because a view holds no data, dropping it never deletes rows, but anything that uses the view will stop working.

1. Syntax

syntax.sql
DROP VIEW [IF EXISTS] view_name [, view_name ...] [RESTRICT | CASCADE];
  • IF EXISTS turns "view doesn't exist" into a note.
  • Several views can be dropped at once.
  • RESTRICT and CASCADE are accepted but ignored.
  • You need the DROP privilege on each view.
Dropping a view leaves the data untouchedThe view active_members is built on the members table. DROP VIEW active_members removes only the saved query; the members table and all its rows remain. Another view built on top of active_members becomes broken.members (table)all rows kept ✔active_membersview: dropped ✘active_countview on a view: brokenDROP VIEW active_members;Only the saved query is removed; base tables are never affected
Figure 1: DROP VIEW deletes the view definition, not data. Views that depend on it stop working.

2. Dropping a View

query.sql
SHOW FULL TABLES WHERE Table_type = 'VIEW';

Views before

Tables_in_gymTable_type
active_countVIEW
active_membersVIEW
gold_membersVIEW
old_reportVIEW
temp_checkVIEW
query.sql
DROP VIEW old_report;
Output
Query OK, 0 rows affected

The data in members is untouched:

query.sql
SELECT COUNT(*) AS members_rows FROM members;

Result: 1 row

members_rows
4

3. Save the Definition First

Keep a copy of the view's SQL before dropping it, so you can recreate it if needed:

query.sql
SHOW CREATE VIEW gold_members;

Result: 1 row

ViewCreate View
gold_membersCREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `gold_members` AS select `members`.`name` AS `name` from `members` where (`members`.`plan` = 'gold')
mysqldump --no-data dbname also exports all view definitions.

4. Dropping Several Views

drop-many.sql
DROP VIEW gold_members, temp_check;
SHOW FULL TABLES WHERE Table_type = 'VIEW';

Result: 2 rows

Tables_in_gymTable_type
active_countVIEW
active_membersVIEW

If one name in the list doesn't exist, the statement fails and, in MySQL 8.0 and later, no view is dropped: the statement is atomic:

partial.sql
CREATE VIEW v_a AS SELECT 1 AS x;
CREATE VIEW v_b AS SELECT 2 AS x;
DROP VIEW v_a, v_missing, v_b;
Output
Query OK, 0 rows affected
Query OK, 0 rows affected
ERROR 1051 (42S02): Unknown table 'gym.v_missing'
query.sql
SHOW FULL TABLES WHERE Table_type = 'VIEW';

v_a and v_b still exist

Tables_in_gymTable_type
active_countVIEW
active_membersVIEW
v_aVIEW
v_bVIEW

Older MySQL versions (5.7) dropped the existing views and reported the missing one. Use IF EXISTS to make the result the same everywhere.

query.sql
DROP VIEW v_a, v_b;
Output
Query OK, 0 rows affected

5. IF EXISTS

query.sql
DROP VIEW IF EXISTS v_missing, active_count;
SHOW WARNINGS;
Output
Query OK, 0 rows affected, 1 warning

Result: 1 row

LevelCodeMessage
Note1051Unknown table 'gym.v_missing'

The existing view active_count was dropped; the missing one only produced a note.

6. Dependent Views Break

MySQL doesn't stop you dropping a view that other views use. Recreate the dependent setup and drop the inner view:

dependent.sql
CREATE VIEW active_count AS SELECT plan, COUNT(*) AS members FROM active_members GROUP BY plan;
DROP VIEW active_members;
SELECT * FROM active_count;
Output
Query OK, 0 rows affected
Query OK, 0 rows affected
ERROR 1356 (HY000): View 'gym.active_count' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them

Find broken views across a database with CHECK TABLE:

query.sql
CHECK TABLE active_count;

Result: 2 rows

TableOpMsg_typeMsg_text
gym.active_countcheckErrorView 'gym.active_count' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
gym.active_countcheckerrorCorrupt

7. Finding Views That Use a Table or View

Before dropping, search the definitions of other views:

query.sql
SELECT table_name AS view_name
FROM information_schema.views
WHERE table_schema = 'gym' AND view_definition LIKE '%active_members%';

Result: 1 row

view_name
active_count

8. DROP VIEW Won't Drop a Table

The statements are kept separate to prevent accidents:

query.sql
DROP VIEW members;
Output
ERROR 1347 (HY000): 'gym.members' is not VIEW

Likewise, DROP TABLE on a view name fails with "Unknown table".

9. Before You Drop a View

  1. Save its definition (SHOW CREATE VIEW).
  2. Search for dependent views, stored routines and application queries that use it.
  3. Check who has privileges on it: SELECT * FROM information_schema.table_privileges WHERE table_name = 'v';
  4. Drop it, then run CHECK TABLE on remaining views to catch breakage.

10. Summary

  • DROP VIEW v; removes a view's definition; base table data is never affected.
  • Drop several views at once; use IF EXISTS in scripts.
  • Dependent views are not checked and will break; find them in information_schema.views.
  • Save the definition first with SHOW CREATE VIEW.