×

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

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 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 use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 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.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

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...

4 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

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.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 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 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.

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.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.