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


为了检查表在 MySQL 中是否存在,你可以使用 INFORMATION_SCHEMA.TABLES。让我们先创建一个表 -

mysql> create table Client_information
   -> (
   -> Id int,
   -> Name varchar(10)
   -> );
Query OK, 0 rows affected (0.48 sec)

以下是使用 insert 命令在表中插入一些记录的查询 -

mysql> insert into Client_information values(1,'Larry');
Query OK, 1 row affected (0.14 sec)

mysql> insert into Client_information values(2,'Mike');
Query OK, 1 row affected (0.17 sec)

mysql> insert into Client_information values(3,'Sam');
Query OK, 1 row affected (0.19 sec)

以下是使用 select 语句从表中显示所有记录的查询 -

mysql> select * from Client_information;

这将产生以下输出 -

+------+-------+
| Id   | Name  |
+------+-------+
| 1    | Larry |
| 2    | Mike  |
| 3    | Sam   |
+------+-------+
3 rows in set (0.00 sec)

以下是检查表在 MySQL 中是否存在或不存在的查询 -

mysql> select * from information_schema.tables where table_name='Client_information';

以下输出说明,表 “Client_information“存在 -

+---------------+--------------+--------------------+------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------------+
| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME         | TABLE_TYPE | ENGINE | VERSION | ROW_FORMAT | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | MAX_DATA_LENGTH | INDEX_LENGTH | DATA_FREE | AUTO_INCREMENT | CREATE_TIME | UPDATE_TIME | CHECK_TIME | TABLE_COLLATION | CHECKSUM | CREATE_OPTIONS | TABLE_COMMENT |
+---------------+--------------+--------------------+------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------------+
| def           | sample       | client_information | BASE TABLE | InnoDB | 10      | Dynamic    | 4          | 4096           | 16384       | 0 | 0 | 0 | NULL | 2019-03-11 20:02:40 | NULL | NULL | utf8_general_ci | NULL | | |
| def           | test         | client_information | BASE TABLE | InnoDB | 10      | Dynamic    | 3          | 5461           | 16384       | 0 | 0 | 0 | NULL | 2019-04-03 02:28:31 | 2019-04-03 02:29:00 | NULL | utf8mb4_0900_ai_ci | NULL | | |
+---------------+--------------+--------------------+------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------------+
2 rows in set (0.13 sec)

更新于: 30-07-2019

771 浏览

开启你的 职业生涯

完成课程并获得证书

开始
广告