MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

MySQL If Exists

Introduction

The two distinct provisions in MySQL are called EXISTS, and IF EXISTS. The records in the subqueries are checked for existence using the EXISTS clause. The EXISTS clause returns true if there is at least one entry in the subquery; otherwise, it returns false. The primary query uses this as the determining factor for running and getting records. On the other hand, when deleting tables, views, and databases, we use the IF EXISTS clause. An error may be avoided by using the IF EXISTS clause at that point if the object we are attempting to dump does not exist. When MySQL detects a missing object, it stops the execution and raises a warning.

How to use the DROP DATABASE, DROP TABLE, and DROP VIEW commands in MySQL using the IF EXISTS clause, with examples and syntax.

1. Drop Database IF EXISTS Command

Syntax:

DROP DATABASE [IF EXISTS] name_of_database;

Name_of_database indicates the database name you want to erase and its components, such as tables, stored procedures, triggers, etc.

If the database called "name_of_database" does not exist on the database server and the DROP DATABASE command is used in MySQL, an error is raised saying that no such database is present. Nevertheless, if the command contains the IF EXISTS condition, a notification rather than an error is generated, indicating that the database with the given name is not present.

The execution of the DROP DATABASE command ends in certain circumstances. The IF EXISTS clause's goal is to stop an error from being raised if a database with the given name does not exist.

First, we must use the following command to open the MySQL connection:

 mysql -u root -p

supplied if you want to use root as your username. If a password is given, a prompt will then appear for the password.

MySQL If Exists

The following output will appear when you run the command mentioned above:

The following command must then be run to list every database on your MySQL database server.

SHOW DATABASES;

In my instance, it produces the following result:

MySQL If Exists

Let's now attempt to remove the database that doesn't exist on our database server and see the results:

DROP DATABASE educate;

The outcome of running the command above is as follows:

An exception with error number 1008 is generated, signifying that the 'educate' database cannot be discarded as it does not exist.

Let's now examine the result of our query statement with the IF EXISTS clause added.

IF education IS EXISTING, DROP DATABASE;

The results of doing the query above are shown as follows:

Even in cases where our database does not include the "educate" database, the report makes it clear that there is no mistake. That indicates the query was executed successfully, with just one warning and no records impacted.

2. Drop-Table IF EXISTS Command

Syntax:

DROP TABLE [IF EXISTS] name_of_table;

Here, name_of_table aids in identifying the name of the database you want to remove, together with all its components, including tables and the structure that includes partitions, constraints, and indexes.

If the database does not have a table with the name "name_of_table," MySQL will give an error stating that it does not exist when you use the DROP TABLE command. The table with the given name is missing, but if you use the IF EXISTS clause in the command, a caution rather than an error is generated. The DROP TABLE command's execution ends in these circumstances. When there isn't a table with the given name, the IF EXISTS clause prevents the error from being raised.

3. Drop View IF EXISTS Command

Syntax:

DROP VIEW [IF EXISTS] name_of_view ;

Here, name_of_view aids in identifying the name of the view and any contents you want to remove. Remember that the original table or tables on which the view definition is based are unaffected.

When using the DROP VIEW command in MySQL, an error stating that no such view is available will be thrown if the idea with the given name (name_of_view) does not exist in the database. A warning indicating that the picture with the given name is not present will be produced in place of an error, however, if the command contains the IF EXISTS condition. At that moment, the execution of the DROP VIEW command ends. When there isn't a view with the given name, the IF EXISTS clause prevents an error from being raised.

Example

Utilizing the following query statement and the basic DROP command, let's attempt to drop the view called viewDemo, which is not in the database named Educate we are now utilizing.

DROP VIEW viewed;

The results of doing the query above are shown as follows:

MySQL If Exists

Since this table does not exist in the EducBA database, an error is raised stating that it is unknown. Though they are not physically present in memory, views are logical tables. When displaying errors and warnings, MySQL also refers to them as tables. Try using the same query statement's IF EXISTS clause now.

DROP VIEW IF EXISTS viewed;

When the query above is run, the following output is generated: a warning and an indication that the question was successfully conducted without altering any rows.

MySQL If Exists

We may run the following command to observe the warning:

SHOW WARNINGS;

That, when the query statement is executed, produces the following output:

MySQL If Exists