SQL Drop Database Statement

In this lesson, you will learn how to delete a database using SQL.

Drop database in SQL

The DROP DATABASE statement is used to delete an existing SQL database.


Syntax

DROP DATABASE database_name;

You should replace database_name with the name of the database that you want to delete.

Delete database example

The following SQL statement deletes the database "demo" that we created in the previous lesson.

Example

DROP DATABASE demo;
    

Output

0 row(s) affected

The output means that the statement is executed succesfully.

Check if the database is deleted

You can use the following SQL statement to view a list of all existing databases. If the "demo" database is not showing that means it was deleted.

Example

SHOW DATABASES;
    

Output

Database
sakila
sys
world
....

The output means that the database is successfully deleted.

If the deleted database is still appears in the MySQL Workbench navigator, just click on the refresh button.

Best way to delete a database

It's recommended to use the optional clause IF EXISTS to be sure that the statement will execute only if the database is exist.

If you try to delete the "demo" database again without the optional clause IF EXISTS, you will get an error as follows.

Example

DROP DATABASE demo;
    

Output

Error Code: 1008. Can't drop database 'demo'; database doesn't exist

However, if you try to delete the "demo" database with the optional clause IF EXISTS, you will get a warning message instead of the error message.

Example

DROP DATABASE IF EXISTS demo;
    

Output

1 warning(s): 1008 Can't drop database 'demo'; database doesn't exist

Tutorials

Online Tools

Sections

Tutorials
Tools
Posts