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
2009-07-01 05:32:26
Just be aware that this is case sensitive!
a_m0d
2009-07-01 05:35:25
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
2009-07-01 05:56:19
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
2009-07-01 05:47:44
+2
A:
Use INFORMATION_SCHEMA:
select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'MyTable';
Should be portable across most databases.
Seth
2009-07-01 06:28:34