SQL DROP DATABASE is used to per­ma­nent­ly delete an entire database including all tables and entries. The SQL command should therefore only be used if the database is actually no longer required or cor­re­spond­ing backups exist. It is also possible to remove several databases at the same time.

What is SQL DROP DATABASE?

When working with Struc­tured Query Language, your initial steps typically involve creating a new database using SQL CREATE DATABASE. Next, create one or more tables with SQL CREATE TABLE, which you can then fill with data. To prevent obsolete databases from occupying storage space or po­ten­tial­ly leaving sensitive data un­pro­tect­ed, it’s important to delete them when they are no longer needed. For this, SQL provides the SQL DROP DATABASE command, which removes the entire database and all its con­nec­tions.

It is important that you take the necessary pre­cau­tions in advance. Once a database has been deleted with this command, all data is ir­re­triev­ably lost. Therefore, make sure that you create backups using the SQL BACKUP DATABASE command. You should also make sure that no important programs or processes rely on the database. The deletion process typically can only be executed by an ad­min­is­tra­tor. Therefore, if you want to use the SQL DROP DATABASE command, you’ll need the necessary per­mis­sions.

VPS Hosting
VPS hosting at un­beat­able prices on Dell En­ter­prise Servers
  • 1 Gbit/s bandwidth & unlimited traffic
  • Minimum 99.99% uptime & ISO-certified data centers
  • 24/7 premium support with a personal con­sul­tant

Syntax and function

The syntax of SQL DROP DATABASE is as follows:

DROP DATABASE name_of_database;
sql

You only need to insert the name of the cor­re­spond­ing database and execute the code. This will remove the database, along with all its tables and entries, making it in­ac­ces­si­ble.

Example of the deletion process

In practice, SQL DROP DATABASE command might look like this. Suppose we have a database called “Customers,” but its entries are outdated. Since we’ve already created a new database with the necessary data, we want to remove the entire old database rather than deleting each table in­di­vid­u­al­ly. The command would be:

DROP DATABASE Customers;
sql

Com­bi­na­tion with IF EXISTS

If you’re unsure whether a par­tic­u­lar database exists, you can use SQL DROP DATABASE with the IF EXISTS command. This command first checks whether a database with the cor­re­spond­ing name exists. If it does, the database is deleted; if not, the command simply does nothing, avoiding an error message. For our example above, the command would be:

DROP DATABASE IF EXISTS Customers;
sql

Delete multiple databases

To delete several databases at the same time, simply list them with commas. SQL DROP DATABASE could then look like this:

DROP DATABASE Customers, Employees, Branches;
sql

In this case, you would remove the Customers, Employees and Branches databases at the same time.

Display existing databases

To check which databases are on your system, use the SHOW DATABASE command. This will list all databases. If the deletion was suc­cess­ful, the removed databases will no longer appear in the list. This command is an effective method for verifying that the SQL DROP DATABASE command was executed correctly. The syntax is as follows:

SHOW DATABASES;
sql

Similar commands to SQL DROP DATABASE

In many cases, you may not want to delete the entire database but only in­di­vid­ual entries or tables. SQL offers several commands for this purpose. To remove an entire table, use DROP TABLE. If you want to keep the table but empty it com­plete­ly, TRUNCATE TABLE is the right choice. You can also use the SQL DELETE statement can also be used to empty a table entirely, and it allows for con­di­tions so that only specific columns or entries are affected if needed.

Tip

MSSQL, MySQL or MariaDB? With SQL Server Hosting from IONOS, you get to choose and benefit from strong per­for­mance, extensive security features and expert, personal advice.

Go to Main Menu