Search code examples
mysqlsql

Check if MySQL table exists without using "select from" syntax?


Is there a way to check if a table exists without selecting and checking values from it?

That is, I know I can go SELECT testcol FROM testtable and check the count of fields returned, but it seems there must be a more direct / elegant way to do it.


Solution

  • If you want to be correct, use INFORMATION_SCHEMA.

    SELECT * 
    FROM information_schema.tables
    WHERE table_schema = 'yourdb' 
        AND table_name = 'testtable'
    LIMIT 1;
    

    Alternatively, you can use SHOW TABLES

    SHOW TABLES LIKE 'yourtable';
    

    If there is a row in the resultset, table exists.