English 中文(简体)
SQL Tutorial

5. 图瓦卢

Selected Reading

SQL - Drop Database
  • 时间:2024-11-03

SQL - DROP or DELETE Database


Previous Page Next Page  

The DROP DATABASE statement in SQL is used to delete a database along with all the data such as tables, views, indexes, stored procedures, and constraints.

The Following are the most important points to remember before we are trying to delete the database −

    It is important to make sure that we have to perform the backup of the database that we are going to delete because once the “DROP DATABASE” statement is executed, all the data and database objects in the database will be permanently deleted and cannot be recovered.

    It is also important to ensure that no other user or apppcation is currently connected to the database that we want to delete. If we try to delete the database while others users are connected to it, then it can cause data corruption or other issues.

Note − Make sure you have the necessary privilege before deleting any database using the DROP DATABASE statement.

Syntax

Following is the syntax to delete a database in SQL

DROP DATABASE DatabaseName;

Here, the “DatabaseName” is the name of the database that we want to delete.

Always the database name should be unique within the RDBMS.

Example

First of all, let us create multiple databases into database system using the following query −

SQL> CREATE DATABASE testDB1;
CREATE DATABASE testDB2;
CREATE DATABASE testDB3;
CREATE DATABASE testDB4;

Let us verify whether the databases are created or not using the following query −

SQL> SELECT * FROM SYS.DATABASES
+--------------------+
| Database           |
+--------------------+
| master             |
| tempdb             |
| model              |
| msdb               |
| testDB1            |
| testDB2            |
| testDB3            |
| testDB4            |  
+--------------------+
8 rows in set (0.00 sec)

Now, let us try to delete a single existing database <testDB> using the DROP DATABASE statement −

SQL> DROP DATABASE testDB1;

Once we have deleted the <testDB> database, we can verify whether it is deleted or not using the following query −

SQL> SELECT * FROM sys.databases;
+--------------------+
| Database           |
+--------------------+
| master             |
| tempdb             |
| model              |
| msdb               |
| testDB2            |
| testDB3            |
| testDB4            | 
+--------------------+
7 rows in set (0.00 sec)

That s it! we have successfully deleted a database in SQL.

Example

Now, let us try to delete a database that doesn t exist in the database system −

SQL> DROP DATABASE testDB1;

The above query returns an error statement because the database we are trying to delete doesn t exist in the database system −

Cannot drop the database  testDB1 , because it does not exist or you do not have permission.

DROP DATABASE IF EXISTS Statement

The DROP DATABASE IF EXISTS statement in SQL includes a condition to check whether the database exists before trying to delete it. If the database does not exist in the database system, the "DROP DATABASE IF EXISTS" statement does not raise an error, but it simply terminates without taking any action.

Syntax

Following is the syntax of the DROP DATABASE IF EXISTS statement in SQL −

DROP DATABASE IF EXISTS DatabaseName;

Here, the “DatabaseName” is the name of the database that we want to delete.

Example

Let us try to delete an existing database <testDB2> in the database system using the following query statement −

SQL> DROP DATABASE IF EXISTS testDB2;

On executing the given program, the output is displayed as follows −

Commands completed successfully.

Let us verify whether the database <testDB2> is deleted or not using the following query −

SQL> SELECT * FROM SYS.DATABASES
+--------------------+
| Database           |
+--------------------+
| master             |
| tempdb             |
| model              |
| msdb               |
| testDB3            |
| testDB4            |  
+--------------------+
6 rows in set (0.00 sec)

Dropping the database that doesn t exist

If you try to drop the database that doesn t exist

Example

Let us try to delete a database <testDB2> that doesn t exist in the database system using the following query statement −

SQL> DROP DATABASE IF EXISTS testDB2;

When we execute the program above, the output is obtained as follows −

Commands completed successfully.

Deleting multiple databases

You can also drop multiple databases at once using the DROP DATABASE statement.

Example

Let us try to delete multiple databases from the database system using the following query −

SQL> DROP DATABASE testDB3, testDB4;

Once we have deleted the databases, we can verify whether they are deleted in the database system or not using the following query −

SQL> SELECT * FROM sys.databases;
+--------------------+
| Database           |
+--------------------+
| master             |
| tempdb             |
| model              |
| msdb               |
+--------------------+
4 rows in set (0.00 sec)
Advertisements