How to Get Record Count for All Tables in MySQL Database
Last Updated :
19 Jun, 2024
In DBMS, counting records for all tables within a MySQL database is a fundamental requirement. Understanding the size and distribution of data across tables helps optimize database performance and provides insights into data utilization and growth patterns.
Row or record count means how many records are present in the database. Here we will discuss various methods to efficiently retrieve record counts from all tables within a MySQL database.
How to Get Record Counts for All Tables
The following methods can be used to get the record count for all tables in MySQL.
- Using INFORMATION_SCHEMA.TABLES with SUM function to Get Record Count
- Using COUNT(*) to Get Record Count
- Using UNION to Get Record Count
These methods differ in query complexity and execution time. INFORMATION_SCHEMA.TABLES with SUM function might be a complex query but have less execution time, whereas COUNT(*) and UNION are simple queries but need more execution time.
Demo MySQL Database
First, we've to create a Database with the help of the query below we'll name it 'demodb'.
CREATE DATABASE demodb;
We'll now create a table named 'gfg' using the following code which defines the table structure with columns such as ‘Name,’ ‘Designation,’ and ‘Age,’ as Columns.
CREATE TABLE gfg
(
Name VARCHAR(50),
Designation VARCHAR(50),
Age INT
);
INSERT INTO gfg (Name, Designation, Age)
VALUES
('Bishal Paul', 'GFG Writer', 21),
('Charles', 'MERN Geek', 35),
('Priyanka', 'SDE', 26);
We'll also create a second table named 'article' using the following code which defines the table structure with columns such as ‘Title,’ ‘Date,’ ‘Written By’ and ‘Age,’ as Columns.
CREATE TABLE article
(
Title VARCHAR(50),
Date Date,
WrittenBy INT,
Age INT
);
INSERT INTO article (Title, Date, WrittenBy, Age)
VALUES
('How to get record counts for all tables', '2024-02-10', 'Bishal' , 22)
Output:
article tableThis approach involves retrieving the sum of the estimated row counts for all tables within the specified database (yourDatabaseName). It operates on the INFORMATION_SCHEMA.TABLES system view, which contains metadata about database tables. The TABLE_ROWS column in this view provides an approximate row count for each table and then the aggregate function SUM gives the total count of rows/records.
Syntax:
SELECT SUM(TABLE_ROWS) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'yourDatabaseName';
Example
The provided SQL query retrieves the sum of estimated row counts for all tables in the 'demodb' database using the INFORMATION_SCHEMA.TABLES view. Here's an explanation of the query:
SELECT SUM(TABLE_ROWS) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'demodb';
Output:
No. of rows in all tables in the database i.e. demodbExplanation: The output of the query provides the estimated total number of rows across all tables in the 'demodb' database, offering a quick insight into the overall data volume.
*Note: For InnoDB tables, the row count is only a rough estimate used in SQL optimization. You'll need to use COUNT(*) for exact counts (which is more expensive).
Using COUNT(*) to Get Record Count
In this method, use various subqueries, i.e., one subquery for each table record count.
Syntax:
SELECT
(SELECT COUNT(*) FROM 'your_table_name1') AS table1Count,
(SELECT COUNT(*) FROM ' your_table_name2') AS table2Count;
Example
The query calculates and displays the counts of records in the 'article' and 'gfg' tables. 'articleCount' indicates the record count in the 'article' table, and 'gfgCount' indicates the record count in the 'gfg' table.
SELECT
(SELECT COUNT(*) FROM article) AS articleCount,
(SELECT COUNT(*) FROM gfg) AS gfgCount;
Output:
No of rows of each tableExplanation: By executing the query, it retrieves and displays the counts of records in the 'article' and 'gfg' tables. The 'articleCount' is the count of records in the 'article' table, and 'gfgCount' is the count of records in the 'gfg' table.
Using UNION to Get Record Count
The UNION operation combines rows from both queries. The operator combines the result returned by the individual SELECT query.
Syntax:
SELECT 'table1' table_names, COUNT(*) AS row_count FROM ' table_name'
UNION
SELECT 'table2', COUNT(*) FROM 'table_name';
Example
The query combines and displays the record counts for the 'article' and 'gfg' tables. It uses UNION to present the results with corresponding table names ('article' and 'gfg').
SELECT 'article' table_names, COUNT(*) AS row_count FROM article
UNION
SELECT 'gfg', COUNT(*) FROM gfg;
Output:
Rows Count of Each TableExplanation: The output provides a unified result of record counts for both the 'article' and 'gfg' tables. Each row displays a table name ('article' or 'gfg') alongside the respective count of records in that table.
Conclusion
This guide explained three distinct approaches to count records in a MySQL database. Using the TABLE_ROWS attribute with the aggregate function SUM and employing a direct count of rows for each table. All approaches offer unique insights into the record counts in the database.
The choice among them depends on the specific requirements of the analysis. If a quick estimate of the overall data volume is sufficient, Approach 1 provides a convenient solution. However, for detailed and accurate record counts at the table level, Approach 2 and Approach 3 offer a more comprehensive approach but at the cost of increased query complexity and execution time.
Similar Reads
How to Get the Datatype of Table Columns in MySQL?
When working with MySQL databases, knowing the datatype of table columns is essential to maintain data integrity and avoid errors. This guide covers methods to check column datatypes, such as using SHOW COLUMNS and querying INFORMATION_SCHEMA.COLUMNS. Understanding column datatypes helps in designin
4 min read
How to Export Database and Table Schemas in SQLite?
Exporting database schemas in SQLite is an important task for database management, enabling functions like data backup, recovery, migration, and auditing. In this article, We will go through the process of exporting database and table schemas in SQLite by understanding various examples to manage SQL
4 min read
How to Show/List Tables in MySQL Database
In MySQL, the SHOW TABLES command is a powerful tool used to list the tables within a specific database. This command provides a convenient way to view the tables that exist in a database without needing to query the database schema directly. In this article, we are going to explore various ways whe
5 min read
How to Show Schema of a Table in MySQL Database?
A table schema in MySQL database defines the structure of table, including columns, data types, relationships between columns, etc. It is a blueprint for the table, describing how data is organized in the table and how it relates to other tables in the database. To see the schema of a table in MySQL
2 min read
How to list the Tables in a SQLite Database File ?
SQLite is a database engine which is written in C programming language. SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is
4 min read
How to Get Counts of all Tables in a Schema in PL/SQL?
In Database Management System, it is essential to retrieve the statistical information about tables with the schema. Whether it is for monitoring the database health, optimizing the performance, or simply understanding the data structures having access to row counts of the tables can be more valuabl
5 min read
How to Show a List of All Databases in MySQL
MySQL is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995. MySQL is rep
7 min read
How to List All Tables in a Schema in Oracle Database?
In Oracle Database, listing all tables within a schema can be crucial for database management and analysis. we can use specific queries to retrieve information about tables in our schema. Below, we explore various queries to list tables, focusing on the SYSOBJECTS view that provides essential metada
3 min read
How to Export MySQL Database using Command Line?
Exporting MySQL databases using the command line utility in Windows is a knowledge every Database Administrator and developer should possess. The mysqldump utility is an easy-to-use program that can back up databases, replicate or transfer data from one server to another and migrate databases. In th
4 min read
How to Get Column Names in MySQL?
To get column names in MySQL use techniques such as the DESCRIBE statement, INFORMATION_SCHEMA.COLUMNS, and SHOW COLUMNS FROM commands. Here will cover these techniques, with explained examples, and help to get a better understanding on how to get column names in MySQL. MySQL Fetch Column Names from
3 min read