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

Note: Syntax within square brackets is optional.

where,

  • IF EXISTS ensures that MySQL does not throw an error if the database doesn’t exist.
  • database_name is the name of the database.

Alternatively, you can also use the following syntax to delete a MySQL database

because according to MySQL Glossary

Physically, a schema is synonymous with a database. You can substitute the keyword SCHEMA instead of DATABASE in MySQL SQL syntax, for example using CREATE SCHEMA instead of CREATE DATABASE.

Let’s see some examples for better understanding.

Example

First, let’s list all the available databases using SHOW DATABASE; statement.

database list before deletion

Now let’s try deleting the ‘programmerdb‘ database using the DROP statement which we have learned before.

Result

database list after deletion

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.

Leave a Reply