Updated June 3, 2023
Introduction to MySQL IF EXISTS
In Mysql, EXISTS and IF EXISTS are the two different provisions. EXISTS clause is used to test the presence of the records in the subqueries. If the subquery contains at least one record the EXISTS clause returns true else returns false. The main query utilizes this as the deciding factor for executing and retrieving records. In contrast, we use the IF EXISTS clause when dropping databases, tables, and views. Using the IF EXISTS clause at that time in case the object we are trying to drop does not exist prevents the occurrence of an error. MySQL terminates the execution as soon as it recognizes the absence of an entity and issues a warning.
How to Use MySQL IF EXISTS?
In this article, we will learn how to use the IF EXISTS clause in MySQL while using DROP DATABASE, DROP TABLE, and DROP VIEW commands, along with syntax and examples.
1. Drop Database IF EXISTS Command
Syntax:
DROP DATABASE [IF EXISTS] name_of_database;
Where name_of_database helps specify the database name you wish to delete completely, including its contents like tables, stored procedures, triggers, etc.
When executing the DROP DATABASE command in MySQL, if no database named “name_of_database” is present in the database server, it throws an error stating that no such database is present. However, if the IF EXISTS clause is used in the command, a notice is raised instead of an error, indicating that the database with the specified name is absent. In such cases, the execution of the DROP DATABASE command terminates. The purpose of the IF EXISTS clause is to prevent the issuance of an error when a database with the specified name does not exist.
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 must execute the following command to list all the databases 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:
We can observe that an error with error code 1008 is thrown, indicating that the ‘educba’ database cannot be dropped due to the absence of such a database.
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 –
The output indicates that the absence of an error is evident even when the database named “educba” does not exist in our database. This suggests a successful query execution, with no affected rows and the presence of one warning.
2. Drop Table IF EXISTS Command
Syntax:
DROP TABLE [IF EXISTS] name_of_table;
Where name_of_ table helps specify the name of the database that you wish to delete, including its contents like tables and its structure consisting of indexes, partitions, constraints, etc.
When you execute the DROP TABLE command in MySQL, and there is no table named “name_of_table” in the database, it throws an error indicating that no such table is present. However, if you use the IF EXISTS clause in the command, a warning is raised instead of an error, stating that the table with the specified name is absent. In such cases, the execution of the DROP TABLE command terminates. The IF EXISTS clause prevents the issuance of an error when a table with the specified name does not exist.
Example
Let us try to drop the table named existdemo, which does not exist in the database named educba that we are using currently using the following query statement of simple DROP command –
DROP TABLE existdemo;
The output of the execution of the above query is as shown below –
It raises an error saying this table is unknown as it does not exist on the educba database. Let us try using the IF EXISTS clause in the same query statement –
DROP TABLE IF EXISTS existdemo;
Upon executing the above query, the output indicates that the query has been successfully executed without impacting any rows, accompanied by a warning.
To see the warning, we can execute the following command –
SHOW WARNINGS;
that gives the following output after the execution of the query statement –
Please note that the above DROP TABLE queries yield the same output irrespective of whether the IF EXISTS clause is present or not, as long as the table being deleted exists in the database. The same applies to the dropping of the database and view.
3. Drop View IF EXISTS Command
Syntax:
DROP VIEW [IF EXISTS] name_of_view;
Where name_of_ view helps specify the name of the view you wish to delete, including its contents. Note that the view definition does not affect the original table(s) on which it is based.
When executing the DROP VIEW command in MySQL, if no view with the specified name (name_of_view) exists in the database, it will throw an error indicating that no such view is present. However, if the command includes the IF EXISTS clause, a warning will be raised instead of an error, stating that the view with the specified name is not present. The execution of the DROP VIEW command terminates at that point. The IF EXISTS clause prevents the issuance of an error when a view with the specified name does not exist.
Example
Let us try to drop the view named viewDemo, which does not exist in the database named educba that we are using currently using the following query statement of simple DROP command –
DROP VIEW viewDemo;
The output of the execution of the above query is as shown below –
It raises an error saying this table is unknown as it does not exist on the educba database. Views are logical tables but are not present physically in memory. Mysql refers to them as tables when issuing errors and warnings as well. Let us try using the IF EXISTS clause in the same query statement –
DROP VIEW IF EXISTS viewDemo;
The execution of the above query produces the following output, indicating that the query has been executed successfully without affecting any rows, along with a warning.
To see the warning, we can execute the following command –
SHOW WARNINGS;
that gives the following output after the execution of the query statement –
Recommended Articles
We hope that this EDUCBA information on “MySQL IF EXISTS” was beneficial to you. You can view EDUCBA’s recommended articles for more information.