How to delete all views from MySQL database using MySQL query/command?
I've been using this one:
/* DROP ALL VIEWS */SET @views = NULL;SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @views FROM information_schema.views WHERE table_schema = @database_name; -- Your DB name here SET @views = IFNULL(CONCAT('DROP VIEW ', @views), 'SELECT "No Views"');PREPARE stmt FROM @views;EXECUTE stmt;DEALLOCATE PREPARE stmt;
Quoting from MySQL Reference Manual:
DROP VIEW [IF EXISTS] view_name [, view_name] ... [RESTRICT | CASCADE]
DROP VIEW
removes one or more views. You must have the DROP
privilege for each view. If any of the views named in the argument list do not exist, MySQL returns an error indicating by name which non-existing views it was unable to drop, but it also drops all of the views in the list that do exist.
The IF EXISTS
clause prevents an error from occurring for views that don't exist. When this clause is given, a NOTE
is generated for each nonexistent view. See Section 12.7.5.41, “SHOW WARNINGS Syntax”.
RESTRICT
and CASCADE
, if given, are parsed and ignored.
try this untested code
DECLARE VIEW_NAME VARCHAR(31); DECLARE VIEW_NAMES CURSOR FOR SELECT table_name FROM information_schema.views; WHERE table_schema = 'DB_Name'OPEN VIEW_NAMES; REPEAT FETCH VIEW_NAMES INTO VIEW_NAME; DROP VIEW VIEW_NAME UNTIL done END REPEAT; CLOSE VIEW_NAMES; END;