Introduction to Delete Database MySQL
We can delete the database and all the contents of the database when they are no more useful by using the DROP DATABASE command. This not only deletes the contents but also all the schema related information stored about the database completely 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 not possible if you do not have any backup of it as the contents are deleted permanently by the DROP DATABASE command.
Syntax
DROP DATABASE [IF EXISTS] name_of_database;
Explanation: where name_of_database helps in specifying the name of the database that you wish to delete completely including its contents like tables, stored procedures, triggers, etc. Whenever DROP DATABASE command is executed and there is no database named name_of_database present in your MySQL database server then it throws an error saying no such database present while executing this command. In that case, if we have used the IF EXISTS clause in the command then a notice is raised instead of error saying database with the specified name is not present and the execution of the DROP DATABASE command is terminated there itself. IF EXISTS clause is used for the prevention of issuing an error when a database with the specified name does not exist.
Examples to Implement Delete Database MySQL
Below are the examples of 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 out all the databases that are present 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: As it can be seen that an error with error code 1008 is thrown saying that ‘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 educba named database doesn’t exist on our database. It shows that the query was fine and no rows were affected and show 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 drop database command:
Code:
DROP DATABASE IF EXISTS educba;
Output:
Explanation: It can be concluded that the query executed successfully without any errors or warnings and returns two rows that were affected. The DROP DATABASE command gives the return value as the number of the tables that were present in that database and got deleted when dropping the database.
An alternative to DROP DATABASE
In MySQL, the database and schema are used interchangeably which means both are referred to the same thing and are synonyms of each other. 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 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: It can be seen that the database is dropped successfully and that no rows are returned and affected as mysqlDropDemo database contained no tables in it.
You can also confirm the presence and dropping of databases at each stage while performing this task by using the SHOW DATABASES; command that gives the list of all the databases present on your database server at that particular moment when this command is executed.
Conclusion
We can delete the database using the SQL command DROP DATABASE that 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 not possible to restore the dropped database if you don’t have any backup of it. There is an alternative command of DROP SCHEMA that can be used to delete the database that exists on your database server. It functions in the same fashion as schemas and databases are treated the same and are synonyms of each other in MySQL. The IF EXISTS clause can be used in both DROP DATABASE and DROP SCHEMA commands to prevent from raising an error if none of the databases exists with the name specified in your DROP query.
Recommended Articles
This is a guide to Delete Database MySQL. Here we discuss an introduction to Delete Database MySQL, syntax, alternatives and examples. You can also go through our other related articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses