How To Drop MySQL Database?

MySQL is a very popular opensource database it is used by a lot of different types and size companies. In this post, we will look at how to drop or delete MySQL databases?

Login MySQL Database

Dropping/Deleting/Removing a database in MySQL server is a simple and fast solution that will delete data as well as table and database schemes. We will command to the local MySQL server daemon whit root privileges.

$ sudo mysql -u root
Login MySQL Database
Login MySQL Database

List Databases

We will list existing databases to get the database names and do not make a mistake.

show databases;
List Databases
List Databases

Drop Database

We will drop the database names test. The syntax of the drop is simple like below. We issue command DROP DATABASE and provide database name as in this example test.

DROP DATABASE test;
Drop Database
Drop Database

Drop All Tables In A Database

Another alternative is dropping all tables one by one or with a simple script. Here database will be not removed but the tables of the database will be deleted.

SET FOREIGN_KEY_CHECKS = 0; 
SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables
 FROM information_schema.tables 
 WHERE table_schema = 'database_name'; -- specify DB name here.

SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;

Leave a Comment