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. This is used in the main query as the deciding factor for the execution and retrieval of records. IF EXISTS clause on the other hand is used while dropping the databases, tables, and views. When we use the IF EXISTS clause at that time in case if the object we are trying to drop does not exist prevents the occurrence of error and the execution is terminated as soon as MySQL recognizes the absence of entity and issues a warning.
How to Use MySQL IF EXISTS?
In this article, we will learn about how we can use the IF EXISTS clause while using DROP DATABASE, DROP TABLE, and DROP VIEW command along with syntax and examples.
1. Drop Database IF EXISTS Command
Syntax:
DROP DATABASE [IF EXISTS] name_of_database;
where name_of_database helps in specifying the name of the database that you wish to dele3te 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.
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 show that one warning is there.
2. Drop Table IF EXISTS Command
Syntax:
DROP TABLE [IF EXISTS] name_of_table;
where name_of_ table helps in specifying the name of the database that you wish to delete completely including its contents like tables and its structure consisting indexes, partition, constraints, etc.
Whenever DROP TABLE command is executed and there is no table named name_of_table present in your MySQL database then it throws an error saying no such table present while executing this command. In that case, if we have used the IF EXISTS clause in the command then a warning is raised instead of error saying table with the specified name is not present and the execution of the DROP TABLE command is terminated there itself. IF EXISTS clause is used for the prevention of issuing 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 exists on educba database. Let us try using IF EXISTS clause in the same query statement –
DROP TABLE IF EXISTS existdemo;
The output of the execution of the above query is as shown below that says that the query is executed successfully without affecting any rows along with 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 –
Note that if the table that is being deleted exists in the database then both the above DROP TABLE queries give the same output and have no effect with the presence or absence of IF EXISTS clause in the statement. The same applies for the droping of database and view.
3. Drop View IF EXISTS Command
Syntax:
DROP VIEW [IF EXISTS] name_of_view;
where name_of_ view helps in specifying the name of the view that you wish to delete completely including its contents. Note that the original table(s) on which the view is defined remain unaffected.
Whenever DROP VIEW command is executed and there is no view named name_of_view present in your MySQL database then it throws an error saying no such view present while executing this command. In that case, if we have used the IF EXISTS clause in the command then a warning is raised instead of error saying view with the specified name is not present and the execution of the DROP VIEW command is terminated there itself. IF EXISTS clause is used for the prevention of issuing 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 exists on the educba database. Views are logical tables but are not present physically on memory they are also referred to as tables in Mysql while issuing errors and warnings. Let us try using IF EXISTS clause in the same query statement –
DROP VIEW IF EXISTS viewDemo;
The output of the execution of the above query is as shown below that says that the query is executed successfully without affecting any rows along with 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
This is a guide to MySQL IF EXISTS. Here we also discuss the introduction and how to use mysql if exists? 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