
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to check if database is empty or not?
You can use INFORMATION_SCHEMA.COLUMNS to check if a database is empty or not. The syntax is as follows −
SELECT COUNT(DISTINCT `TABLE_NAME`) AS anyAliasName FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `table_schema` = 'yourDatabaseName';
The above syntax returns 0 if the database has notable otherwise it returns the number of tables. For our example, we are using the databases ‘sample’ and ‘test3’, which we created before.
The first database ‘sample’ has more tables, therefore the above query will return a number of tables. The second database ‘test3’ does not have any tables, therefore the above query will return 0.
Case 1 − Database sample
The query is as follows −
mysql> SELECT COUNT(DISTINCT `table_name`) AS TotalNumberOfTables FROM `information_schema`.`columns` WHERE `table_schema` = 'sample';
The following is the output displaying the number of tables in it, therefore the database isn’t empty −
+---------------------+ | TotalNumberOfTables | +---------------------+ | 130 | +---------------------+ 1 row in set (0.01 sec)
Case 2 − Database test3
The query is as follows −
mysql> SELECT COUNT(DISTINCT `table_name`) AS TotalNumberOfTables FROM `information_schema`.`columns` WHERE `table_schema` = 'test3';
The following is the output returning 0, therefore the database is empty −
+---------------------+ | TotalNumberOfTables | +---------------------+ | 0 | +---------------------+ 1 row in set (0.00 sec)
As mentioned above, if we get 0, that would mean there are no tables in the database.
- Related Articles
- Check if a table is empty or not in MySQL using EXISTS
- Java Program to check if a string is empty or not
- Python program to check if the string is empty or not
- How to check if field is null or empty in MySQL?
- Which query is efficient to check if MySQL Table is empty? COUNT(*) vs. LIMIT?
- How to Check if an Array is Empty or Not in Java
- How to check if a text field is empty or not in swift?
- How to check if the Azure resource group is empty or not using PowerShell?
- How do you check if a ResultSet is empty or not in JDBC?
- How do I check if a column is empty or null in MySQL?
- How to check an empty table already in a MySQL database?
- C# program to check whether a list is empty or not
- Python program to check whether a list is empty or not?
- Golang Program to check the given string is empty or not
- How to check an array is empty or not using jQuery?
