×

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples.

The DROP command removes all the objects stored in the database and database from the SQL server. The DROP query removes the database permanently from the SQL Server. If tables and views stored in the database are important for you, then the data backup needs to be kept in another database.

Following are the important key points to remember before deleting the database from the SQL Server:

1 The DROP query removes all the records or objects from the database and database from the Server. If data stored inside the database is usable in the future, we must keep the backup of objects stored inside the database, which we are deleting.

2 The most important key point is that we cannot remove the database from the SQL Server, which the database users use. While using the database, we try to remove the database, and then the output display is as follows:

Database Cannot be dropped.

The syntax of the DROP DATABASE query is as follows:

DROP DATABASE Database_Name;

We need to mention the database name in the SQL server, and we want to remove the remove permanently from the SQL Server. The database name needs to mention just after the DROP DATABASE keyword.

The below syntax is used to remove multiple databases from the SQL Server is as follows:

DROP DATABASE Database_Name_1, Database_Name_2, Database_Name_3, Database_Name_N;

Examples of DROP Database in SQL

We will execute the SHOW DATABASES command to check which databases are residing inside the SQL Server before removing the databases from the SQL Server.

SHOW DATABASES;
Database
Cricket
Demo
Demodb
Employee
Ids
information_schema
Javatpoint
Mumbai
mysql
performance_schema
Phpadmin
Registration
Student
student_1
Testdb
WordPress
World
SQL DROP Database

Example 1: Write a query to remove the cricket name database from the SQL Server.

DROP DATABASE Cricket; 

In the above DROP DATABASE query example, we remove the cricket database name from the SQL Server.

We will execute the SHOW DATABASES query to check whether the cricket name database is successfully removed or not is as follows:

SHOW DATABASES;
Database
Demo
Demodb
Employee
Ids
information_schema
Javatpoint
Mumbai
mysql
performance_schema
Phpadmin
Registration
Student
student_1
Testdb
WordPress
World
SQL DROP Database

As we can see, the cricket name database is successfully removed from the SQL Server.

Example 2: Write a query to remove the demo name database from the SQL Server.

DROP DATABASE Demo;

In the above DROP DATABASE query example, we remove the Demo name database from the SQL Server.

We will execute the SHOW DATABASES query to check whether the demo name database is successfully removed or not is as follows:

SHOW DATABASES;
Database
Demodb
Employee
Ids
information_schema
Javatpoint
Mumbai
mysql
performance_schema
Phpadmin
Registration
Student
student_1
Testdb
WordPress
World
SQL DROP Database

We dropped a database without adding arguments or parameters in the above examples.

1. IF EXISTS

IF EXISTS is one of the parameters, we can use it while removing the database in the SQL Server. Suppose there are several databases in the SQL Server, and we don't know whether the database is removing resides in the SQL Server. The purpose of the IF EXISTS parameter is to check whether the database we are going to drop is already removed or not in the SQL Server.

The syntax of the DROP DATABASE with the IF EXISTS parameter is as follows:

DROP DATABASE IF EXISTS DataBase_Name;

If the database exists, then the query will remove the database from the SQL Server, but if the database doesn't exist, the warning output is displayed.

Example 1: Write a query to remove the database with the IF EXISTS parameter.

DROP DATABASE IF EXISTS Demodb;

In the above DROP DATABASE query example, we dropped the database with the IF EXISTS parameter database name demodb. The query will remove the database if the database resides in the SQL Server. The warning output is displayed if the database is already removed from the SQL Server, and we try to execute the query.

We will execute the SHOW DATABASES query to check whether the demodb name database is successfully removed or not is as follows:

SHOW DATABASES;
Database
Employee
Ids
information_schema
Javatpoint
Mumbai
mysql
performance_schema
Phpadmin
Registration
Student
student_1
Testdb
WordPress
World
SQL DROP Database

Example 2: Write a query to remove the database with the IF EXISTS parameter.

DROP DATABASE IF EXISTS Employee;

In the above DROP DATABASE query example, we dropped the database with the IF EXISTS parameter database name employee. The query will remove the database if the database resides in the SQL Server. The warning output is displayed if the database is already removed from the SQL Server, and we try to execute the query.

We will execute the SHOW DATABASES query to check whether the employee name database is successfully removed or not is as follows:

SHOW DATABASES;
Database
Ids
information_schema
Javatpoint
Mumbai
mysql
performance_schema
Phpadmin
Registration
Student
student_1
Testdb
WordPress
World
SQL DROP Database

Example 3: Write a query to remove the database with the IF EXISTS parameter.

DROP DATABASE IF EXISTS Employee;

In the above DROP DATABASE query example, we dropped the database with the IF EXISTS parameter database name employee. The query will remove the database if the database resides in the SQL Server. The warning output is displayed if the database is already removed from the SQL Server, and we try to execute the query.

The output of the above query is as follows:

SQL DROP Database

As we can see in the output, 1 warning because the database name employee is already dropped from the server.


Related Topics

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.