In this tutorial, we will show you how to Drop or delete a database in SQL Server is using the Drop Database
statement and also using SQL Server Management Studio.
Create two database’s example database sales
& purchase
. You can refer to our previous tutorial How to Create Database in SQL. We will delete the sales
database using the Drop Database
statement. Delete the purchase
database using the SQL Server Management Studio.
Table of Contents
Using the Drop Database
statement
The Syntax of the Drop Database
is as shown below
1 2 3 | Drop Database <DatabaseNAme> |
For Exampe, to delete a sales
database, use the command Drop Database sales
To Delete the Database
- Connect the SQL Server using the SSMS
- Open the Query editor by clicking on the
Toolbar -> New Query
- Enter the Query
Drop database Sales
- Execute the query either by pressing
F5
key or click onExecute
option in the toolbar. Commands completed Successfully
message appears in the message window.- Refresh the object Explorer.
Drop Database Using SQL Server Management Studio
Another way to drop a database is by using the GUI tool of SSMS
- Connect the SQL Server using the SSMS
- Select the database to delete
- Right Click and select Delete
- In the Delete Object window, click OK to delete the database
Drop Multiple Database
You can drop multiple database in a single command. The syntax is
Drop Database <databaseName1>, <databaseName2>
Create two databases Sales
& Purchase
as shown below
1 2 3 4 | create database Sales create database Purchase |
You can drop both of them using
1 2 3 | Drop Database Sales, Purchase |
Drop Database if Exists
IF EXISTS Applies to: SQL Server 2016 and above
If you try to delete a non-existent database, you will see the following error message. For Example, run the command drop database hr
and you will see the following error message
Cannot drop the database 'HR'
, because it does not exist or you do not have permission.
You can use the IF EXISTS Clause, which will not raise the error. For example, the following query will drop the database HR, only if exists else it does nothing
1 2 3 | Drop Database If exists HR |
The database cannot be removed if in use
We cannot delete the database, which is in use. This means some user is reading or writing to the database.
For Example run the following query
1 2 3 4 5 6 7 8 | create database Sales //Creates a database Use Sales //Starts Using it Drop Database Sales //Will not work //Can not drop database "Sales" because it is currently in use. |
The above example is a simple use case. You can run the use master
to use the master database, which stops using the sales
database. And then drop the sales
database as shown below
1 2 3 4 5 6 7 | Use Sales //Starts Using it Use Master //Starts using the Master database. Sales is not used now Drop Database Sales //No error |
But if you forcefully remove all the users, who are using the database using the command. SET SINGLE_USER
forces the database be work in Single User mode. It logs out all other users. Now you can safely drop the database.
1 2 3 | ALTER DATABASE Sales SET SINGLE_USER |
Reference
Read More