列出MariaDB数据库中所有表的4种方法
以下是使用 SQL 或命令行列出 MariaDB 数据库中的表的四种方法。
命令SHOW TABLES
该命令列出给定数据库中的非表、序列和视图:SHOW TABLESTEMPORARY
SHOW TABLES;
结果:
+——————–+ | Tables_in_pethouse | +——————–+ | Owners | | PetTypes | | Pets | | vownercount | | vpetcount | | vpetsowners | | vpetstypes | | vpettypecount | +——————–+
这列出了当前数据库(称为)中的所有表。pethouse
我们还可以使用 themodifier 返回显示类型的第二列:FULL
SHOW FULL TABLES;
结果:
+——————–+————+ | Tables_in_pethouse | Table_type | +——————–+————+ | Owners | BASE TABLE | | PetTypes | BASE TABLE | | Pets | BASE TABLE | | vownercount | VIEW | | vpetcount | VIEW | | vpetsowners | VIEW | | vpetstypes | VIEW | | vpettypecount | VIEW | +——————–+————+
我们可以看到,这个数据库中的大多数“表”实际上是视图。
如果我们只想返回基表,我们可以使用 aclause:WHERE
SHOW FULL TABLES
WHERE Table_type = 'BASE TABLE';
结果:
+——————–+————+ | Tables_in_pethouse | Table_type | +——————–+————+ | Owners | BASE TABLE | | PetTypes | BASE TABLE | | Pets | BASE TABLE | +——————–+————+
SHOW TABLES还接受可用于将结果缩小到仅与给定模式匹配的表的子句:LIKE
SHOW TABLES
LIKE 'pet%';
结果:
+—————————+ | Tables_in_pethouse (pet%) | +—————————+ | PetTypes | | Pets | +—————————+
命令SHOW TABLE STATUS
命令类似于命令,但提供有关每个(非)表的更广泛信息。SHOW TABLE STATUS``SHOW TABLES``TEMPORARY
它也接受aandclause,喜欢。WHERE``LIKE``SHOW TABLES
例:
SHOW TABLE STATUS
LIKE 'pet%';
结果:
+———-+——–+———+————+——+—————-+————-+—————–+————–+———–+—————-+———————+————-+————+——————–+———-+—————-+———+——————+———–+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | Max_index_length | Temporary | +———-+——–+———+————+——+—————-+————-+—————–+————–+———–+—————-+———————+————-+————+——————–+———-+—————-+———+——————+———–+ | PetTypes | InnoDB | 10 | Dynamic | 4 | 4096 | 16384 | 0 | 0 | 0 | NULL | 2021-03-30 09:10:36 | NULL | NULL | utf8mb4_general_ci | NULL | | | 0 | N | | Pets | InnoDB | 10 | Dynamic | 8 | 2048 | 16384 | 0 | 32768 | 0 | NULL | 2021-04-01 15:42:43 | NULL | NULL | utf8mb4_general_ci | NULL | | | 0 | N | +———-+——–+———+————+——+—————-+————-+—————–+————–+———–+—————-+———————+————-+————+——————–+———-+—————-+———+——————+———–+
该表information_schema.TABLES
我们还可以查询表:information_schema.TABLES
SELECT
TABLE_NAME
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA LIKE 'pethouse' AND TABLE_TYPE LIKE 'BASE_TABLE';
结果:
+————+ | TABLE_NAME | +————+ | Pets | | Owners | | PetTypes | +————+
查询此内容而不筛选结果会从所有数据库中返回表。同样,在不筛选的情况下查询它也会返回所有表类型。TABLE_SCHEMA``TABLE_TYPE
客户mariadb-show
另一种方法是使用实用程序。mariadb-show
要使用此选项,请打开命令行提示符/终端窗口并运行以下命令(替换为您感兴趣的数据库):pethouse
mariadb-show pethouse;
结果:
+—————+ | Tables | +—————+ | Owners | | PetTypes | | Pets | | vownercount | | vpetcount | | vpetsowners | | vpetstypes | | vpettypecount | +—————+
这将返回视图和表。
输出仅显示您具有某些权限的数据库、表或列的名称。
如果未给出数据库,则显示所有匹配的数据库。如果未给出表,则显示数据库中所有匹配的表。如果未给出列,则显示表中所有匹配的列和列类型。
客户端也可以按以下方式运行:mysqlshow
mysqlshow pethouse;
该实用程序接受相当多的选项,例如(以便您可以传递用户名),(以便您可以传递密码)等。--user``--password
有关选项的完整列表,请参阅MariaDB 的文档。