MySQL SHOW TABLES: List Tables In a MySQL Database

摘要:在本教程中,您将学习如何使用 MySQL SHOW TABLES 命令来查询特定数据库中的表。

MySQL SHOW TABLES

要列出 MySQL 数据库中的表,请按照下列步骤操作:

  1. 使用MySQL客户端(例如mysql登录MySQL数据库服务器
  2. 使用USE语句切换到特定数据库。
  3. 使用SHOW TABLES命令。

下面说明了 MySQL SHOW TABLES命令的语法:

SHOW TABLES;
Code language: SQL (Structured Query Language) (sql)

MySQL 显示表示例

以下示例向您展示如何列出classicmodels数据库中的表。

步骤1.连接MySQL数据库服务器:

>mysql -u root -p
Enter password: **********
mysql>
Code language: SQL (Structured Query Language) (sql)

步骤2.切换到classicmodels数据库:

mysql> use classicmodels;
Database changed
mysql>
Code language: SQL (Structured Query Language) (sql)

步骤 3. 显示classicmodels数据库中的表:

> show tables;
+-------------------------+
| Tables_in_classicmodels |
+-------------------------+
| customers               |
| employees               |
| offices                 |
| orderdetails            |
| orders                  |
| payments                |
| productlines            |
| products                |
+-------------------------+
8 rows in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)

SHOW TABLES命令允许您显示表是基表还是视图。要在结果中包含表类型,请使用以下形式的SHOW TABLES语句。

SHOW FULL TABLES;
Code language: SQL (Structured Query Language) (sql)

让我们在classicmodels数据库中创建一个名为contacts的视图,其中包括employeescustomers表中的名字、姓氏和电话号码以进行演示。

CREATE VIEW contacts 
AS 
SELECT lastName, firstName, extension as phone 
FROM employees 
UNION
SELECT contactFirstName, contactLastName, phone 
FROM customers;
Code language: SQL (Structured Query Language) (sql)

现在,您发出SHOW FULL TABLES命令:

> SHOW FULL TABLES
+-------------------------+------------+
| Tables_in_classicmodels | Table_type |
+-------------------------+------------+
| contacts                | VIEW       |
| customers               | BASE TABLE |
| employees               | BASE TABLE |
| offices                 | BASE TABLE |
| orderdetails            | BASE TABLE |
| orders                  | BASE TABLE |
| payments                | BASE TABLE |
| productlines            | BASE TABLE |
| products                | BASE TABLE |
+-------------------------+------------+
9 rows in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)

正如您所看到的,除了作为视图的contacts表之外,所有表都是基表。

对于有很多表的数据库,一次显示所有表可能不太直观。

幸运的是, SHOW TABLES命令为您提供了一个选项,允许您使用LIKE运算符或WHERE子句中的表达式来过滤返回的表,如下所示:

SHOW TABLES LIKE pattern;

SHOW TABLES WHERE expression;
Code language: SQL (Structured Query Language) (sql)

例如,要显示classicmodels数据库中以字母p开头的所有表,请使用以下语句:

> SHOW TABLES LIKE 'p%';
+------------------------------+
| Tables_in_classicmodels (p%) |
+------------------------------+
| payments                     |
| productlines                 |
| products                     |
+------------------------------+
3 rows in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)

或者要显示以字符串'es'结尾的表,请使用以下语句:

> SHOW TABLES LIKE '%es';
+-------------------------------+
| Tables_in_classicmodels (%es) |
+-------------------------------+
| employees                     |
| offices                       |
| productlines                  |
+-------------------------------+
3 rows in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)

以下语句说明如何使用SHOW TABLES语句中的WHERE子句列出classicmodels数据库中的所有视图。

> SHOW FULL TABLES WHERE table_type = 'VIEW';
+-------------------------+------------+
| Tables_in_classicmodels | Table_type |
+-------------------------+------------+
| contacts                | VIEW       |
+-------------------------+------------+
1 row in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)

有时,您希望查看数据库中未连接到的表。在这种情况下,您可以使用SHOW TABLES语句的FROM子句来指定要显示其中的表的数据库。

以下示例演示如何显示以'time'开头的表;

> SHOW TABLES FROM mysql LIKE 'time%';
+---------------------------+
| Tables_in_mysql (time%)   |
+---------------------------+
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
+---------------------------+
5 rows in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)

以下语句与上面的语句等效,但它使用IN而不是FROM

SHOW TABLES IN mysql LIKE 'time%';
Code language: SQL (Structured Query Language) (sql)

请务必注意,如果您没有基表或视图的权限,则它不会显示在SHOW TABLES命令的结果集中。

在本教程中,您学习了如何使用 MySQL SHOW TABLES语句列出特定数据库中的所有表。

本教程有帮助吗?