EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL IF EXISTS

MySQL IF EXISTS

MySQL IF EXISTS

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

MySQL IF EXISTS-1.1

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:

MySQL IF EXISTS-1.2

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:

MySQL IF EXISTS-1.3

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 –

MySQL IF EXISTS-1.4

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 –

Output-1.5

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.

Output-1.6..

To see the warning, we can execute the following command –

SHOW WARNINGS;

that gives the following output after the execution of the query statement –

Output-1.7

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 –

Output-1.8

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.

Output-1.9

To see the warning, we can execute the following command –

SHOW WARNINGS;

that gives the following output after the execution of the query statement –

Output-1.10

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.

  1. MySQL IF Function
  2. UUID in MySQL
  3. MySQL sum()
  4. MySQL Partition
C++ PROGRAMMING Certification Course
38+ Hours of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Certification Course
149+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL - Everything in SQL
253+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Certification Course
74+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MYSQL Certification Course
 115+ Hours of HD Videos
18 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more