How To Drop MySQL Database?
MySQL is very popular opensource database it is used by a lot of different type and size companies. In this post we will look how to drop or delete MySQL databases?
Login MySQL Database
Dropping/Deleting/Removing a database in MySQL server is simple and fast solution which 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
We will list existing databases to get database name and do not make mistake.
We will drop database names test. Syntax of drop is simple like below. We issue command DROP DATABASE and provide database name as in this example test.
DROP DATABASE test;
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
WHERE table_schema = 'database_name'; -- specify DB name here.
SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;