tags:

views:

44

answers:

3

I want to confirm whether there is a certain table. When create a table, there is an SQL sentence such as DROP TABLE IF EXISTS xxx_tb. Will there be the method that can identify the existence of the table by SQL likewise?

+1  A: 

You want the SHOW TABLES command of MySQL:

SHOW TABLES LIKE 'xxx_tb';
artlung
Just be aware that this is case sensitive!
a_m0d
It's always best to be aware of case, but on systems without case sensitivity, that's not true: http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html
artlung
A: 

Or indeed, you can just do a query like

SELECT COUNT(*) FROM tbl WHERE 1=0

Which will give an error (see documentation for exact error code, or try it) if the table doesn't exist, but succeed with no results if it does.

MarkR
+2  A: 

Use INFORMATION_SCHEMA:

select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'MyTable';

Should be portable across most databases.

Seth