MySQL - SHOW OPEN TABLES 语句
SHOW OPEN TABLES 语句
CREATE TABLE 语句用于在 MYSQL 数据库中创建表。您需要指定表的名称以及每列的定义(名称和数据类型)。
SHOW OPEN TABLES 语句显示当前在表缓存中打开的非临时表列表。除了表名之外,此语句还提供表所在数据库的名称以及二进制值(0 或 1),用于指定该表是否正在使用以及是否被锁定。
语法
以下是 SHOW OPEN TABLES 语句的语法 -
SHOW OPEN TABLES
[{FROM | IN} db_name]
[LIKE 'pattern' | WHERE expr]
示例
以下语句显示当前在表缓存中打开的表的信息 -
SHOW OPEN TABLES\G;
输出
上述查询产生以下输出 -
************* 1. row *************
Database: mysql
Table: check_constraints
In_use: 0
Name_locked: 0
************* 2. row *************
Database: mysql
Table: column_type_elements
In_use: 0
Name_locked: 0
************* 3. row *************
Database: mysql
Table: slave_master_info
In_use: 0
Name_locked: 0
************* 4. row *************
Database: mysql
Table: foreign_keys
In_use: 0
Name_locked: 0
************* 5. row *************
Database: mysql
Table: columns
In_use: 0
Name_locked: 0
************* 6. row *************
Database: mysql
Table: foreign_key_column_usage
In_use: 0
Name_locked: 0
************* 7. row *************
Database: mysql
Table: server_cost
In_use: 0
Name_locked: 0
************* 8. row *************
Database: mysql
Table: index_column_usage
In_use: 0
Name_locked: 0
************* 9. row *************
Database: mysql
Table: view_table_usage
In_use: 0
Name_locked: 0
************* 10. row *************
Database: mysql
Table: index_partitions
In_use: 0
Name_locked: 0
************* 11. row *************
Database: mysql
Table: indexes
In_use: 0
Name_locked: 0
************* 12. row *************
Database: mysql
Table: schemata
In_use: 0
Name_locked: 0
************* 13. row *************
Database: mysql
Table: collations
In_use: 0
Name_locked: 0
************* 14. row *************
Database: mysql
Table: table_partition_values
In_use: 0
Name_locked: 0
************* 15. row *************
Database: mysql
Table: table_partitions
In_use: 0
Name_locked: 0
************* 16. row *************
Database: mysql
Table: tables
In_use: 0
Name_locked: 0
************* 17. row *************
Database: mysql
Table: role_edges
In_use: 0
Name_locked: 0
************* 18. row *************
Database: mysql
Table: triggers
In_use: 0
Name_locked: 0
************* 19. row *************
Database: test
Table: contact
In_use: 0
Name_locked: 0
************* 20. row *************
Database: mysql
Table: column_statistics
In_use: 0
Name_locked: 0
........................
........................
........................
FROM 或 IN 子句
您可以使用 FROM 子句从特定数据库中检索当前在表缓存中打开的非 TEMPORARY 表的信息。
SHOW OPEN TABLES FROM performance_schema;
输出
以下是上述查询的输出 -
| Database | Table | In_use | Name_locked |
|---|---|---|---|
| performance_schema | events_waits_history_long | 0 | 0 |
| performance_schema | session_status | 0 | 0 |
| performance_schema | events_stages_history_long | 0 | 0 |
| performance_schema | events_statements_current | 0 | 0 |
| performance_schema | session_variables | 0 | 0 |
| performance_schema | threads | 0 | 0 |
您也可以使用 IN 子句代替 FROM,如下所示:-
SHOW OPEN TABLES IN performance_schema;
输出
执行上述查询后,将产生以下输出:-
| Database | Table | In_use | Name_locked |
|---|---|---|---|
| performance_schema | events_waits_history_long | 0 | 0 |
| performance_schema | session_status | 0 | 0 |
| performance_schema | events_stages_history_long | 0 | 0 |
| performance_schema | events_statements_current | 0 | 0 |
| performance_schema | session_variables | 0 | 0 |
| performance_schema | threads | 0 | 0 |
LIKE 子句
使用 LIKE 子句,您可以指定一种模式来检索表缓存中当前打开的特定表的信息。以下查询检索名称以单词 "information" 开头的表的描述。
SHOW OPEN TABLES LIKE 'time%';
输出
查询执行后,将生成如下所示的输出 -
| Database | Table | In_use | Name_locked |
|---|---|---|---|
| mysql | time_zone_transition | 0 | 0 |
| mysql | time_zone_leap_second | 0 | 0 |
| mysql | time_zone_name | 0 | 0 |
| mysql | time_zone | 0 | 0 |
| mysql | time_zone_transition_type | 0 | 0 |
WHERE 子句
您可以使用 SHOW OPEN TABLES 语句的 WHERE 子句来检索符合指定条件的变量的描述。
示例
假设我们创建了一个包含销售详情的表,如下所示 -
CREATE TABLE SalesDetails ( ID INT, ProductName VARCHAR(255), CustomerName VARCHAR(255), DispatchDate date, DeliveryTime time, Price INT, Location VARCHAR(255), );
现在创建另一个包含客户联系方式的表格,创建表格如下 -
CREATE TABLE CustContactDetails ( ID INT, Name VARCHAR(255), Age INT, Phone BIGINT, Address VARCHAR(255), Email VARCHAR(50) );
以下查询显示当前正在使用的打开的表 -
SHOW OPEN TABLES WHERE In_use=1;
输出
上述查询生成以下输出 -
| Database | Table | In_use | Name_locked |
|---|---|---|---|
| demo | custcontactdetails | 1 | 0 |
| demo | salesdetails | 1 | 0 |

