We learned how to create a database in MySQL in our earlier post but we didn’t discuss how to delete an existing database in MySQL.
So in this post let’s discuss everything about deleting an existing database in MySQL.
The general syntax to delete a database in MySQL is
DROP DATABASE [IF EXISTS] database_name;
Note: Syntax within square brackets is optional.
IF EXISTSensures that MySQL does not throw an error if the database doesn’t exist.
database_nameis the name of the database.
Alternatively, you can also use the following syntax to delete a MySQL database
DROP SCHEMA [IF EXISTS] database_name;
because according to MySQL Glossary
Physically, a schema is synonymous with a database. You can substitute the keyword
DATABASEin MySQL SQL syntax, for example using
CREATE SCHEMAinstead of
Let’s see some examples for better understanding.
First, let’s list all the available databases using
SHOW DATABASE; statement.
Now let’s try deleting the ‘programmerdb‘ database using the DROP statement which we have learned before.
DROP DATABASE programmerdb;
As a result of executing the
DROP DATABASE statement, we get the number of rows affected. In our case, it is 1 which means that the ‘programmerdb’ database had only 1 table into it.
And if we list the database once again using
SHOW DATABASE; statement, we notice ‘programmerdb’ is no longer available.
That’s all for this small tutorial. I hope now you can easily delete an existing database in MySQL using the
DROP DATABASE statement. If you have any doubts or suggestion then please comment below.