牛骨文教育服务平台(让学习变的简单)
博文笔记

mysql 查看数据库中所有表的记录数

创建时间:2016-08-25 投稿人: 浏览次数:183

mysql使用select count(*) from table_name可以查询某个表的总记录数。想快速的知道数据库中所有表的记录数信息怎么办?如果使用mysql的版本在5.0及以上,可以通过查询information_schema库中的tables表来获取,该表中使用table_rows记录表的行数信息。例如查看库testdb中所有表的记录数:

use information_schema;

select table_name,table_rows from tables 
where TABLE_SCHEMA = "testdb" 
order by table_rows desc; 

不过需要注意的是,对于InnoDB表,table_rows行计数仅是大概估计值。

另外一种办法还是借助information_schema库的tables表,来拼接出一个条sql语句,例如:

use information_schema;

select concat(
    "select "", 
    TABLE_name, 
    "", count(*) from ", 
    TABLE_SCHEMA, 
    ".",
    TABLE_name,
    " union all"
) from tables 
where TABLE_SCHEMA="testdb";

把生成的结果手动加工一下就行了,起码比一张张表去拼写要来的快。

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。