Here, I summarize some of the fastest way to check a Table in MySQL is empty or not.
- By querying on Database [information_schema]
This is the fastest and the most I recommended to be applied, because in the database [information_schema] stored name, number of recordings and others are always up-to-date each time the operation occurred on both the MySQL Database system, Table, Trigger, etc.» Syntax
set @rows = (select TABLE_ROWS
from information_schema.TABLES
where TABLE_SCHEMA = ‘database_name’
and TABLE_NAME = ‘table_name’
and TABLE_TYPE = ‘BASE TABLE’);select @rows;
» Parameters
‘database_name’, please change in the quote with the name of the database from the Table which will be checked!
‘table_name’, please change in the quote with the name of Table which will be checked!
‘BASE TABLE’, as standard type of MySQL Database and Table.» Returns
if @rows = 0 then table is empty.
if @rows = null then database or table not found.
if @rows > 0 then total existing row aka table is not empty. - By counting records of a Table
By counting the amount of data from a Table is about to be checked and for me, this is silly but effective way, but not recommended if adjusted to the title of this article.» Syntax
set @rows = (select count(*) from ‘database_name’.’table_name’);select @rows;
» Parameters
‘database_name’, please change in the quote with the name of the database from the Table which will be checked!
‘table_name’, please change in the quote with the name of Table which will be checked!» Returns
if @rows = 0 then table is empty.
if @rows > 0 then total existing row aka table is not empty.
January 7, 2010 at 6:44 AM
Nice post. Liking this blog going to have to bookmark it.