Query to count the number of tables I have in MySQL

Mysql

Mysql Problem Overview


I am growing the number of tables I have and I am sometimes curious just to do a quick command line query to count the number of tables in my database. Is that possible? If so, what is the query?

Mysql Solutions


Solution 1 - Mysql

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

Source

This is mine:

USE databasename; 
SHOW TABLES; 
SELECT FOUND_ROWS();

Solution 2 - Mysql

In case you would like a count all the databases plus a summary, please try this:

SELECT IFNULL(table_schema,'Total') "Database",TableCount 
FROM (SELECT COUNT(1) TableCount,table_schema 
      FROM information_schema.tables 
      WHERE table_schema NOT IN ('information_schema','mysql') 
      GROUP BY table_schema WITH ROLLUP) A;

Here is a sample run:

mysql> SELECT IFNULL(table_schema,'Total') "Database",TableCount
    -> FROM (SELECT COUNT(1) TableCount,table_schema
    ->       FROM information_schema.tables
    ->       WHERE table_schema NOT IN ('information_schema','mysql')
    ->       GROUP BY table_schema WITH ROLLUP) A;
+--------------------+------------+
| Database           | TableCount |
+--------------------+------------+
| performance_schema |         17 |
| Total              |         17 |
+--------------------+------------+
2 rows in set (0.29 sec)

Give it a Try !!!

Solution 3 - Mysql

SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'dbo' and TABLE_TYPE='BASE TABLE'

Solution 4 - Mysql

This will give you names and table count of all the databases in you mysql

SELECT TABLE_SCHEMA,COUNT(*) FROM information_schema.tables group by TABLE_SCHEMA;

Solution 5 - Mysql

To count number of tables just do this:

USE your_db_name;    -- set database
SHOW TABLES;         -- tables lists
SELECT FOUND_ROWS(); -- number of tables

Sometimes easy things will do the work.

Solution 6 - Mysql

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

Solution 7 - Mysql

There may be multiple ways to count the tables of a database. My favorite is this on:

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

Solution 8 - Mysql

select name, count(*) from DBS, TBLS 
where DBS.DB_ID = TBLS.DB_ID 
group by NAME into outfile '/tmp/QueryOut1.csv' 
fields terminated by ',' lines terminated by '\n';

Solution 9 - Mysql

from command line :

mysql -uroot -proot  -e "select count(*) from 
information_schema.tables where table_schema = 'database_name';"

in above example root is username and password , hosted on localhost.

Solution 10 - Mysql

SELECT COUNT(*) FROM information_schema.tables

Solution 11 - Mysql

> mysql> show tables;

it will show the names of the tables, then the count on tables.

source

Solution 12 - Mysql

Hope this helps, and returns only number of tables in a database

Use database;

SELECT COUNT(*) FROM sys.tables;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGenadinikView Question on Stackoverflow
Solution 1 - MysqlJoseadrianView Answer on Stackoverflow
Solution 2 - MysqlRolandoMySQLDBAView Answer on Stackoverflow
Solution 3 - MysqlMukundView Answer on Stackoverflow
Solution 4 - MysqlRohit DubeyView Answer on Stackoverflow
Solution 5 - MysqljmmView Answer on Stackoverflow
Solution 6 - MysqlArun KasyakarView Answer on Stackoverflow
Solution 7 - MysqlautomatixView Answer on Stackoverflow
Solution 8 - MysqlSwamyView Answer on Stackoverflow
Solution 9 - MysqlRajiv SinghView Answer on Stackoverflow
Solution 10 - MysqlShanView Answer on Stackoverflow
Solution 11 - MysqlMohammed NosiratView Answer on Stackoverflow
Solution 12 - MysqlmjohnbatchaView Answer on Stackoverflow