如何检测 MySQL 中是否存在表?

mysqlmysqli database更新于 2023/10/24 9:41:00

要检测表是否存在,请使用 INFORMATION_SCHEMA.TABLES 的概念。以下是语法 −

select table_name from information_schema.tables
where table_schema=database()
and table_name=yourTableName;

为了理解上述语法,让我们创建一个表 −

mysql> create table DemoTable2032
   -> (
   -> ClientId int,
   -> ClientName varchar(20),
   -> ClientAge int,
   -> ClientCountryName varchar(20)
   -> );
Query OK, 0 rows affected (1.07 sec)

这是检测数据库中是否存在表的查询 −

mysql> select table_name from information_schema.tables
   -> where table_schema=database()
   -> and table_name='DemoTable2032';

这将产生以下输出 −

+---------------+
| TABLE_NAME    |
+---------------+
| demotable2032 |
+---------------+
1 row in set (0.00 sec)

相关文章