Introduction of MySQL Drop Database
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 dropped 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. In this article, we will learn the syntax of the DROP DATABASE command and its usage with the help of examples.
Syntax:
DROP DATABASE [IF EXISTS] name_of_database;
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 of MySQL Drop Database
Firstly we will have to open the MySQL connection using the command:
mysql -u root -p
provided if you wish to login with root as the username and further, it will prompt for the password if any password is set.
Executing the above command will give you the following output:
Then you will have to execute the following command to list out all the databases that are present in your MySQL database server.
SHOW DATABASES;
that gives the following output in my case:
Now, let us try to delete the database that is not present on our database server and see the outputs –
DROP DATABASE educba;
Executing the above command gives the following output:
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.
Now, let us study the output after adding the IF EXISTS clause in our query statement –
DROP DATABASE IF EXISTS educba;
The output of the execution of the above query is as shown below:
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 shows that one warning is there.
Now, let us create one database named educba using CREATE DATABASE command –
CREATE DATABASE educba;
that gives the following output:
Now let us create two tables named to test and development
CREATE TABLE test(
testCaseId INT NOT NULL AUTO_INCREMENT,
testCase VARCHAR(100),
trials INT,
cleared BOOLEAN,
PRIMARY KEY (testCaseId)
);
that gives the following output:
and one more create table query for development that is as follows –
CREATE TABLE development(
moduleId INT NOT NULL AUTO_INCREMENT,
scenario VARCHAR(100),
hoursRequired INT,
selfTested BOOLEAN,
PRIMARY KEY (moduleId)
);
Execution of the above query gives the following output:
Now, let us drop the educba database using the drop database command –
DROP DATABASE IF EXISTS educba;
that now gives the following output:
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.
Let us try to delete the database named mysqlDropDemo using the following query statement –
DROP SCHEMA mysqlDropDemo;
The output of the above query execution is as follows:
It throws an error saying no database named mysqlDropDemo exists in my database server. Now, let us execute the same query with IF EXISTS clause in it –
DROP SCHEMA IF EXISTS mysqlDropDemo;
that gives the following output:
It can be seen that one warning is raised.
Let us create the database named mysqlDropDemo using the following query –
CREATE DATABASE mysqlDropDemo;
that gives the following output:
Now if execute the same query of DROP SCHEMA then the output will be different. We will be executing the following query statement –
DROP SCHEMA IF EXISTS mysqlDropDemo;
that now results in the following output:
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 drop 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. 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 MySQL Drop Database. Here we also discuss the introduction and syntax along with different examples and its code implementation. You may also have a look at the following articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses