Mysql 简明教程
MySQL − Show Tables
MySQL Show Tables Statement
在 MySQL 中,我们使用 SHOW TABLES 命令来检索特定数据库中存在的表的名称。此命令在各种情况下很有用,例如:
In MySQL, we use the SHOW TABLES command to retrieve the names of tables that are present in a specific database. This command is useful in various situations, such as:
-
When we want to view names of tables present in a database to verify if a specific table exists or not.
-
When we want to display additional information about each table present in a database, we use the SHOW TABLES command with the MySQL FULL modifier.
-
Additionally, we can use the SHOW TABLES command with WILDCARDS to filter and display only the tables that match a specific pattern.
Syntax
以下是 MySQL SHOW TABLES 命令的语法:
Following is the syntax of MySQL SHOW TABLES command−
SHOW TABLES;
在继续使用示例之前,假设以下表存在于两个数据库中, testdb1 和 testdb2 :
Before proceeding with the examples, assume that the following tables exist in two databases, testdb1 and testdb2:
Example
首先,我们正在将数据库更改为 testdb1 以对它执行 SHOW TABLES 操作。
First of all, we are changing the database to testdb1 to perform the SHOW TABLES operation on it.
mysql> USE testdb1;
Database changed
现在,执行以下查询以列出 testdb1 数据库中的所有表。
Now, execute the following query to list down all the tables from testdb1 database.
SHOW TABLES;
SHOW TABLES with FULL modifier
在 MySQL 中,我们使用可选 FULL 修饰符和 SHOW TABLES 命令一起显示第二输出列,其中包含有关数据库中表的其他信息,例如它们的类型: BASE TABLE 表示表、 VIEW 表示视图或 SYSTEM VIEW 表示 INFORMATION_SCHEMA 表。
In MySQL, we use the optional FULL modifier along with the SHOW TABLES command to display a second output column that contains additional information about the tables present in a database, such as their types: BASE TABLE for a table, VIEW for a view, or SYSTEM VIEW for an INFORMATION_SCHEMA table.
SHOW TABLES in different Database
在 MySQL 中,我们可以检索另一个数据库中存在的表列表。要做到这一点,我们需要在 SHOW TABLES 语句中使用 IN 运算符或 FROM 子句。
In MySQL, we can retrieve the list of tables present in another database. To do so, we need to use the IN operator or the FROM clause in conjunction with the SHOW TABLES statement.
Example
在下面的查询中,我们使用 SHOW TABLES 命令和 IN 运算符获取存在于另一个数据库 testdb2 中的表列表。
In the following query, we are fetching the list of tables that exist in another database testdb2, using the SHOW TABLES command with IN operator.
SHOW TABLES IN testdb2;
Output
以下是 testdb2 数据库中存在的表名:
Following are the names of the tables that are present in testdb2 database −
SHOW TABLES using Pattern Matching
在某些情况下,当数据库中有大量表,而我们只想检索特定表时,我们会将 LIKE 运算符与 WILDCARD characters (如“%”)一起使用。这些通配符会筛选并仅显示与特定模式匹配的表。
In some scenarios where there are large amount of tables present in a database, and we want to retrieve only specific tables, we use the LIKE operator with WILDCARD characters such as '%'. These wildcards will filter and display only the tables that match a specific pattern."
Example
在下面的查询中,我们使用 SHOW TABLES 命令和 LIKE 运算符来选择 testdb1 数据库中名称以“stud”开头的所有表。
In the following query, we are using the LIKE operator with SHOW TABLES command to select all the tables (in testdb1 database) where the name starts with "stud".
SHOW TABLES IN testdb1 LIKE "stud%";
Output
以下是名称以“stud”开头的 testdb1 数据库中存在的表:
Following are the tables present in testdb1 database whose name starts with "stud" −
Example
在此,我们尝试检索名称以“stud”开头的 testdb2 数据库中的表 -
Here, we are trying to retrieve the tables from testdb2 database where the name starts with "stud" −
SHOW TABLES IN testdb2 LIKE "stud%";
Showing tables Using a Client Program
除了使用 MySQL 查询显示 MySQL 数据库中存在的表列表之外,我们还可以使用客户端程序来执行 SHOW TABLES 操作。
Besides showing the list of tables present in a MySQL database with a MySQL query, we can also use a client program to perform the SHOW TABLES operation.