SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO SELECT

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL

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.



ADVERTISEMENT
ADVERTISEMENT