How do I detect if a table exist in MySQL?


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)

Updated on: 07-Apr-2020

192 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements