Updated June 5, 2023
Introduction to Delete Database MySQL
We can delete the database and all the contents of the database when they are no more helpful by using the DROP DATABASE command. This deletes the contents and all the schema-related information stored about the database entirely from your MySQL database server. You will find no trace of the deleted database. Hence, it is necessary to be extra careful while using this command, as restoration of the database is impossible if you do not have any backup, as the DROP DATABASE command permanently deletes the contents.
Syntax
DROP DATABASE [IF EXISTS] name_of_database;
Explanation: where name_of_database helps specify the name of the database that you wish to delete completely, including its contents like tables, stored procedures, triggers, etc. When you execute the DROP DATABASE command in MySQL, and the database server does not have a database named “name_of_database,” the command will return an error stating that there is no such database. However, you can use the IF EXISTS clause in the command to raise a warning instead of an error. This warning indicates that the database with the specified name does not exist.
Examples to Implement Delete Database MySQL
Below are the examples of the DROP DATABASE command:
Example #1
Firstly we will have to open the MySQL connection using the command:
Code:
mysql -u root -p
Output:
Explanation: provided if you wish to login with root as the username and further, it will prompt for the password if any password is set.
Example #2
Then you will have to execute the following command to list all the databases in your MySQL database server.
Code:
SHOW DATABASES;
Output:
Example #3
Now, let us try to delete the database that is not present on our database server and see the outputs:
Code:
DROP DATABASE educba;
Output:
Explanation: It can be seen that an error with error code 1008 is thrown, saying that the ‘educba’ database cannot be dropped as no such database exists.
Example #4
Now, let us study the output after adding the IF EXISTS clause in our query statement:
Code:
DROP DATABASE IF EXISTS educba;
Output:
Explanation: It can be seen from the output that no error is raised even when the educba named database doesn’t exist on our database. It shows that the query was fine, no rows were affected, and that one warning is there.
Example #5
Now, let us create one database named educba using CREATE DATABASE command:
Code:
CREATE DATABASE educba;
Output:
Example #6
Now let us insert two tables named to test and development:
Code:
CREATE TABLE test(
testCaseId INT NOT NULL AUTO_INCREMENT,
testCase VARCHAR(100),
trials INT,
cleared BOOLEAN,
PRIMARY KEY (testCaseId)
);
Output:
Example #7
One more create table query for development that is as follows:
Code:
CREATE TABLE development(
moduleId INT NOT NULL AUTO_INCREMENT,
scenario VARCHAR(100),
hoursRequired INT,
selfTested BOOLEAN,
PRIMARY KEY (moduleId)
);
Output:
Example #8
Now, let us drop the educba database using the drop database command:
Code:
DROP DATABASE IF EXISTS educba;
Output:
Explanation: The query was completed correctly, with no errors or warnings, and returned two impacted rows. The DROP DATABASE command returned the number of tables in that database and deleted when it was dropped.
Alternative to DROP DATABASE
In MySQL, the terms database and schema are used interchangeably, which means they refer to the same thing and are synonyms. Hence, we can make the use of
DROP SCHEMA [IF EXISTS] name_of_database;
This functions in the same manner as that of the DROP DATABASE command.
Step 1: Let us try to delete the database named mysqlDropDemo using the following query statement:
Code:
DROP SCHEMA mysqlDropDemo;
Output:
Explanation: It throws an error saying no database named mysqlDropDemo exists in my database server.
Step 2: Now, let us execute the same query with the IF EXISTS clause in it:
Code:
DROP SCHEMA IF EXISTS mysqlDropDemo;
Output:
Step 3: Let us create the database named mysqlDropDemo using the following query:
Code:
CREATE DATABASE mysqlDropDemo;
Output:
Step 4: Now, if you execute the same query of DROP SCHEMA, then the output will be different. We will be executing the following query statement:
Code:
DROP SCHEMA IF EXISTS mysqlDropDemo;
Output:
Explanation: Because the “mysqlDropDemo” database contained no tables, the database was successfully dropped, and no records were returned or affected.
Using the SHOW DATABASES, you can also confirm the presence and dropping of databases at each stage while performing this task. When you run this command, the program returns a list of all databases on your database server.
Conclusion
We can delete the database using the SQL command DROP DATABASE, which completely and permanently deletes the database and all the contents of that database. Hence, we should be careful while using this command, as it is impossible to restore the dropped database if you don’t have any backup. You can use an alternative command of DROP SCHEMA to delete the existing database on your database server.
Recommended Articles
We hope that this EDUCBA information on “Delete Database MySQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.