Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if table exists in mysql [duplicate]

Tags:

mysql

Possible Duplicate:
MySQL - check if table exists without using “select from”

Can I rely on this query to find out if tables in the specified database exists or there may be some restrictions?

SELECT
    `information_schema`.`TABLES`.`TABLE_NAME`
FROM
    `information_schema`.`TABLES`
WHERE
    `information_schema`.`TABLES`.`TABLE_SCHEMA` = 'my_database_name'
AND `information_schema`.`TABLES`.`TABLE_NAME` IN (
    'table_name',
    'table_name',
    'table_name',
    'table_name',
    'table_name',
    'table_name'
)

P.S. I do not need to create a table, and just need to check whether it exists or not.

like image 769
Kin Avatar asked Jan 15 '23 14:01

Kin


1 Answers

Or you could use this (longer query).

SELECT count(*)
FROM information_schema.tables
WHERE table_schema = 'databasename'
AND table_name = 'tablename'
like image 187
Jordi Kroon Avatar answered Jan 17 '23 05:01

Jordi Kroon