什么 mysql 命令可以显示数据库中的表以及有多少行?

什么 mysql 命令可以显示数据库中的表以及有多少行?

有没有一个 mysql 命令可以显示表以及表中有多少行?

答案1

从 MySQL 5 开始,您可以查询虚拟表information_schema,其中包含有关 MySQL 数据库中表的元数据。

要找出每个数据库中每个表的行数:

$ mysql -u root -p \
    -e "select table_schema,table_name,table_rows from information_schema.tables;"
+---------------------+---------------------------------------+------------+
| table_schema        | table_name                            | table_rows |
+---------------------+---------------------------------------+------------+
| information_schema  | CHARACTER_SETS                        |       NULL | 
| information_schema  | COLLATIONS                            |       NULL | 
| information_schema  | COLLATION_CHARACTER_SET_APPLICABILITY |       NULL | 
...
...
| arrdb01             | active_part                           |         24 | 
| arrdb01             | audit_record                          |         19 | 
| arrdb01             | code                                  |          8 | 
| arrdb01             | part_obj                              |          0 | 
| arrdb02             | active_part                           |         24 | 
| arrdb02             | audit_record                          |         14 | 
| arrdb02             | code                                  |          9 | 
| arrdb02             | part_obj                              |          1 | 
| cacti               | cdef                                  |          8 | 
| cacti               | cdef_items                            |         22 | 
| cacti               | colors                                |        215 | 
...
...

上面的命令从 information_schema 表中选择 3 列:

  • table_schema(数据库名称)
  • 表名
  • 表行数

要查看它包含的所有字段,您可以使用describe命令:

$ mysql -u root -p -e "describe information_schema.tables"
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| TABLE_CATALOG   | varchar(512) | YES  |     | NULL    |       | 
| TABLE_SCHEMA    | varchar(64)  | NO   |     |         |       | 
| TABLE_NAME      | varchar(64)  | NO   |     |         |       | 
| TABLE_TYPE      | varchar(64)  | NO   |     |         |       | 
| ENGINE          | varchar(64)  | YES  |     | NULL    |       | 
| VERSION         | bigint(21)   | YES  |     | NULL    |       | 
| ROW_FORMAT      | varchar(10)  | YES  |     | NULL    |       | 
| TABLE_ROWS      | bigint(21)   | YES  |     | NULL    |       | 
| AVG_ROW_LENGTH  | bigint(21)   | YES  |     | NULL    |       | 
| DATA_LENGTH     | bigint(21)   | YES  |     | NULL    |       | 
| MAX_DATA_LENGTH | bigint(21)   | YES  |     | NULL    |       | 
| INDEX_LENGTH    | bigint(21)   | YES  |     | NULL    |       | 
| DATA_FREE       | bigint(21)   | YES  |     | NULL    |       | 
| AUTO_INCREMENT  | bigint(21)   | YES  |     | NULL    |       | 
| CREATE_TIME     | datetime     | YES  |     | NULL    |       | 
| UPDATE_TIME     | datetime     | YES  |     | NULL    |       | 
| CHECK_TIME      | datetime     | YES  |     | NULL    |       | 
| TABLE_COLLATION | varchar(64)  | YES  |     | NULL    |       | 
| CHECKSUM        | bigint(21)   | YES  |     | NULL    |       | 
| CREATE_OPTIONS  | varchar(255) | YES  |     | NULL    |       | 
| TABLE_COMMENT   | varchar(80)  | NO   |     |         |       | 
+-----------------+--------------+------+-----+---------+-------+

参考

答案2

connect name_of_database;
show tables;
select count(*) from name_of_database.name_of_table;

要获取合格表名的完整列表:

select concat(table_schema, '.', table_name) from information_schema.tables;

答案3

相关内容