Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a table already exists in the database by using MySQL?

Now I am using something like:

dbResult = dbStatement.executeQuery("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '[e2]' AND table_name = '[emp101_messages]'");
while(dbResult.next())
{
    int value = -1;
    value= dbResult.getInt(1);
    System.out.println(value + " table count");
}

Is this correct way to check if a table exists in a database?

like image 444
Soorya Thomas Avatar asked Dec 26 '22 21:12

Soorya Thomas


1 Answers

SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'dbname'
  AND table_name = 'my_tablename';

+----------------------+
| table_name           |
+----------------------+
| my_tablename |
+----------------------+

As most of the options are already provided. Please see if this can help.

like image 189
metalfight - user868766 Avatar answered May 06 '23 01:05

metalfight - user868766