- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Database
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SQL - DROP or DELETE Database
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 application 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)