Check if a MySQL table exists
by Rekha[ Edit ] 2010-01-28 14:00:55
Using show tables
The easiest way is using the "show tables" function. If your database (called "test" in this example) had three tables name "test1", "test2" and "another_test", running "show tables" would display this:
Tables_in_test
another_test
test1
test2
3 rows in set (0.01 sec)
You can use show tables like this to see if a single table exists:
mysql> show tables like "test1";
which would return:
Tables_in_test (test1)
test1
1 row in set (0.00 sec)
If you ran show tables on a table that didn't exist you would get this:
mysql> show tables like "test3";
Empty set (0.01 sec)