Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all views from MySQL database using MySQL query/command?

I need to delete all views from my MySQL database. How can I do that using query?

Can anyone can help me please?

like image 682
DEVOPS Avatar asked Jan 13 '12 10:01

DEVOPS


People also ask

How do I delete a view in MySQL?

To delete a view, use the DROP VIEW command. DROP VIEW takes one argument: the name of the view to be dropped. A database name can be prepended to the view name. You can add the IF EXISTS syntax.

How do I delete everything in MySQL?

There are two ways to delete all the data in a MySQL database table. TRUNCATE TABLE tablename; This will delete all data in the table very quickly. In MySQL the table is actually dropped and recreated, hence the speed of the query.

Which of the following command can be used to remove view from MySQL?

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.

How can I see all views in MySQL?

To get a list of MySQL views, we can use the SELECT command with LIKE operator. Let us see the syntax first. mysql> SELECT TABLE_SCHEMA, TABLE_NAME -> FROM information_schema.


2 Answers

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;
like image 92
Steven de Salas Avatar answered Oct 24 '22 21:10

Steven de Salas


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.

like image 27
user998692 Avatar answered Oct 24 '22 21:10

user998692