How do I detect if a table exist in MySQL?

MySQLMySQLi Database

To detect the existence of a table, use the concept of INFORMATION_SCHEMA.TABLES. Following is the syntax −

select table_name from information_schema.tables
where table_schema=database()
and table_name=yourTableName;

To understand the above syntax, let us create a table −

mysql> create table DemoTable2032
   -> (
   -> ClientId int,
   -> ClientName varchar(20),
   -> ClientAge int,
   -> ClientCountryName varchar(20)
   -> );
Query OK, 0 rows affected (1.07 sec)

Here is the query to detect if a table exist in a database −

mysql> select table_name from information_schema.tables
   -> where table_schema=database()
   -> and table_name='DemoTable2032';

This will produce the following output −

+---------------+
| TABLE_NAME    |
+---------------+
| demotable2032 |
+---------------+
1 row in set (0.00 sec)
raja
Published on 07-Apr-2020 14:56:56
Advertisements