In MySQL, to get the total number of data for all tables in the database, you can write a query script to iterate through each table and calculate its number of rows. You can use the INFORMATION_SCHEMA database, which contains tables about database metadata, such as TABLES and COLUMNS.
Here is a sample script that shows how to use SQL queries and stored procedures to get the total number of data for all tables:
Using SQL queries and scripts
- Query the number of rows of all tables (direct method, suitable for command line tools)
If you just need this information temporarily and your database tables are not large, you can run the following query directly in the command line tool:
SELECT table_name AS 'Table', table_rows AS 'Rows' FROM information_schema.tables WHERE table_schema = 'your_database_name' AND table_type = 'BASE TABLE';
Willyour_database_name
Replace with your actual database name.table_rows
Columns provide an estimate of the number of rows per table (for InnoDB tables, this may be an estimate, not an exact value).
- Use stored procedures (suitable for more complex scenarios)
If you just want to get the total number of rows for all tables, you can use the following query:
SELECT SUM(table_rows) FROM information_schema.tables WHERE table_schema = 'your_database_name';
This returns the sum of rows of all tables in the database.
- Use stored procedures (suitable for more complex scenarios)
If you need more flexible or complex processing, you can create a stored procedure to iterate through all tables and count their row counts. Here is an example stored procedure:
DELIMITER // -- Set the new statement ending character to "//" so that ";" is used in the stored procedure without ending the definition of the entire stored procedure. CREATE PROCEDURE CountAllTableRows() -- Create a name calledCountAllTableRowsStored procedures。 BEGIN -- Variable declaration part DECLARE done INT DEFAULT FALSE; -- Declare a namedoneinteger variables,Used to mark whether the cursor has been read or not,The initial value isFALSE。 DECLARE tbl_name VARCHAR(255); -- Declare a nametbl_namestring variable,Used to store the table name currently processed。 DECLARE row_count INT DEFAULT 0; -- Declare a namerow_countinteger variables,Used to temporarily store row count(But it was not used in this process)。 -- Cursor declaration part DECLARE cur CURSOR FOR -- Declare a namecurCursor。 SELECT table_name -- Cursor querySQLStatement,frominformation_schema.tablesSelect the table name。 FROM information_schema.tables WHERE table_schema = 'your_database_name' -- Qualified query database name(Need to be replaced with the actual database name)。 AND table_type = 'BASE TABLE'; -- Select only the basic table(Exclude views, etc.)。 -- Continue to process the processor at the end of the cursor DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- When the cursor cannot read the data,set updoneforTRUE。 -- Temporary statement part CREATE TEMPORARY TABLE IF NOT EXISTS temp_row_counts ( -- Create a name calledtemp_row_countsTemporary table(If it does not exist)。 table_name VARCHAR(255), -- Storage table name。 row_count BIGINT -- Store row count,useBIGINTTo support large tables。 ); -- Open the cursor OPEN cur; -- Read loop read_loop: LOOP -- 定义一个名forread_loopThe cycle of。 FETCH cur INTO tbl_name; -- from游标中读取一个表名到tbl_namevariable。 IF done THEN -- Determine whether the reading is completed。 LEAVE read_loop; -- If the reading is complete,Leave the loop。 END IF; -- Get the number of rows in the current table SET @s = CONCAT('SELECT COUNT(*) INTO @cnt FROM ', tbl_name); -- Splicing dynamicsSQLStatement。 PREPARE stmt FROM @s; -- Prepare for execution dynamicsSQLStatement。 EXECUTE stmt; -- Execution dynamicsSQLStatement,The result is stored in@cntvariable中。 DEALLOCATE PREPARE stmt; -- Release dynamicsSQLStatement。 -- Insert row count into temporary table INSERT INTO temp_row_counts (table_name, row_count) VALUES (tbl_name, @cnt); -- Insert table name and row number into temporary table。 END LOOP; -- Close cursor CLOSE cur; -- from临时表中选择结果 SELECT * FROM temp_row_counts; -- Query and display all records in the temporary table。 -- Delete temporary tables DROP TEMPORARY TABLE IF EXISTS temp_row_counts; -- Delete temporary tables(If there is)。 END // -- The stored procedure definition ends. DELIMITER ; -- 将Statement结束符重置for默认的";"。
Then, you can get the number of rows of all tables by calling the stored procedure:
CALL CountAllTableRows();
Notes:
- Before actually using stored procedures, you need to replace ‘your_database_name’ with the actual database name.
- Dynamic SQL is used in stored procedures (through PREPARE and EXECUTE statements) because it is not allowed to directly execute SQL statements containing variables as table names in stored procedures.
- Temporary table temp_row_counts is used to store the number of rows in each table so that all results can be queried at the end of the stored procedure.
- Use the BIGINT type to store the number of rows to support tables that can be very large.
Knock on the blackboard
- performance: For very large databases, these methods can be very time-consuming and resource-consuming.
-
Estimated value: For InnoDB tables,
table_rows
A column may be an estimate, not an exact value. If exact values are required, they must be executedCOUNT(*)
Query. -
Permissions: Make sure your database users have sufficient permissions to access
INFORMATION_SCHEMA
and execute the required query.
Through the above method, you can easily obtain the total number of data for all tables in the MySQL database.
This is the article about MySQL obtaining the total number of table data in the database. For more information about MySQL obtaining the total number of table data, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!