
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- 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 - 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 - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - DROP Database
The SQL DROP DATABASE statement is used to delete an existing database along with all the data such as tables, views, indexes, stored procedures, and constraints.
SQL DROP Database Statement
Following are the important points to remember before you delete an existing database −
- Make sure you have taken proper backup of the database before you delete it.
- Make sure no other application is connected and using this database.
- Make sure you have the necessary privilege to delete the database. Usually an admin can delete the databaase.
Syntax
Following is the syntax to delete a database in SQL −
DROP DATABASE DatabaseName;
Here, the DatabaseName is the name of the database that you want to delete. A database name is always unique within the RDBMS.
Example
First of all, let us create multiple databases into database system using the following SQL queries −
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 −
SHOW DATABASES;
This will list down all the available databases:
Database |
---|
information_schema |
mysql |
performance_schema |
testDB1 |
testDB2 |
testDB3 |
testDB4 |
Now, let us try to delete the testDB1 database using the SQL DROP DATABASE statement −
DROP DATABASE testDB1;
Once we have deleted the testDB1 database, we can verify whether it is deleted or not using the SQL SHOW DATABASES statement −
SHOW DATABASES;
This will list down all the available databases:
Database |
---|
information_schema |
mysql |
performance_schema |
testDB2 |
testDB3 |
testDB4 |
That's it! we have successfully deleted a database in SQL.
SQL DROP DATABASE IF EXISTS Statement
The SQL DROP DATABASE IF EXISTS statement 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 you want to delete.
Example
Let us try to delete an existing database testDB2 in the database system using the following SQL statement −
DROP DATABASE IF EXISTS testDB2;
When we execute the above SQL statement, the output is obtained as follows −
Query OK, 0 rows affected, 3 warnings (0.024 sec)
Dropping the Database that doesn't Exist
Let us try to drop a database testDB2 that doesn't exist in the database system using the following SQL statement −
DROP DATABASE IF EXISTS testDB2;
When we execute the above SQL statement, the output is obtained as follows −
Query OK, 0 rows affected, 1 warning (0.000 sec)
Deleting Multiple Databases
You can drop multiple databases using the SQL DROP DATABASE statement as follows:
DROP DATABASE testDB3, testDB4;