mysqlshow与表的数量

时间:2009-08-15 12:10:25

标签: mysql

以下命令将让我知道数据库的名称。

$ mysqlshow

但是,我如何知道每个数据库中的表数以及空表的数量? 例如。

db count empty

测试10 5

mydb 122 0

客户34 34

5 个答案:

答案 0 :(得分:2)

如果您有权限,可以从information_schema.tables中选择count(*)。

select count(*) from information_schema.tables
where table_schema = <My Schema>
and table_type = 'BASE TABLE';

并在table_rows列上过滤空表:

 select count(*) from information_schema.tables
 where table_schema = <My Schema>
 and table_type = 'BASE TABLE'
 and table_rows = 0;

答案 1 :(得分:1)

SHOW DATABASES;
SHOW TABLES FROM --your_db_here--;
SELECT COUNT(*) = 0 FROM --your_table_here--;

答案 2 :(得分:1)

有一个特殊的数据库information_schema,它包含有关mysql服务器上所有数据库的元数据。

SELECT `TABLE_NAME`, `TABLE_ROWS` 
FROM `information_schema`.`TABLES` 
WHERE `TABLE_SCHEMA` = 'NameOfDatabaseYouAreInterestedIn'
AND `TABLE_TYPE` = 'BASE TABLE'

TABLE_ROWS并不总是完全准确,您可能希望遍历表并获得计数

答案 3 :(得分:0)

您可以使用

SHOW TABLES;

获取数据库中的表,然后计算返回的行数。

你也可以这样做(虽然它可能很慢):

 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'my_database';

答案 4 :(得分:0)

我不知道这是否有帮助,但mysqlshow可以选择显示每个表的行数(--count):

$mysqlshow --count  *p*
Wildcard: %p%
+-------------+--------+--------------+
|  Databases  | Tables |  Total Rows  |
+-------------+--------+--------------+
| implantacao |     25 |          134 |
| pmsp        |     80 |      8561947 |
| tmp         |      7 |            5 |
+-------------+--------+--------------+
3 rows in set.

如果您传递数据库名称:

$mysqlshow --count tmp
Database: tmp
+------------+----------+------------+
|   Tables   | Columns  | Total Rows |
+------------+----------+------------+
| builds     |        2 |          0 |
| gtable     |        2 |          5 |
| patterns   |        9 |          0 |
| products   |        2 |          0 |
| sig_types  |        2 |          0 |
| signatures |        2 |          0 |
| versions   |        2 |          0 |
+------------+----------+------------+
7 rows in set.

您可以添加表名并获取相关信息:

$mysqlshow tmp gtable
Database: tmp  Table: gtable
+--------+---------+-------------------+------+-----+---------+-------+---------------------------------+---------+
| Field  | Type    | Collation         | Null | Key | Default | Extra | Privileges                      | Comment |
+--------+---------+-------------------+------+-----+---------+-------+---------------------------------+---------+
| symbol | text    | latin1_swedish_ci | YES  |     |         |       | select,insert,update,references |         |
| size   | int(11) |                   | YES  |     |         |       | select,insert,update,references |         |
+--------+---------+-------------------+------+-----+---------+-------+---------------------------------+---------+

查看mysqlshow --help了解更多选项/信息。