Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL to check if database is empty (no tables)

Tags:

sql

mysql

I need to check if a database is totally empty (no tables) using an SQL query. How can this be done?

Thanks for the help!

like image 671
EdanB Avatar asked Jun 18 '09 14:06

EdanB


People also ask

How to check if MySQL database is not empty?

Solution: In order to verify your databases is not empty you can watch list of tables and measure instances in it. first: perform simple connection to your db mysql -u <userName> -p ,run show databases; pick your database using use <databaseName>; and then run show tables; and expect to have list of tables.

How to check if a table is empty in MS Access?

I usually write queries in MS Access and did not know I had to make an alias on the subquery so this is very helpful. IF EXISTS can be used for check if a table is empty. IF EXISTS (select * from YourTable) SELECT 'Table is not empty' ELSE SELECT 'Table is empty'

Does select 1 return no rows if the table is empty?

I happen to like this method because it is standard SQL. @Stoleg Because the requirement was to select 1 or 0, and select 1 would return no rows if the table is empty. @mustaccio my apologies! It does! I wanted to write about EXISTS, but Sebastian Meine got it faster. Although I would prefer using EXISTS, there is one more method.

How to count the number of tables in a MySQL database?

first: perform simple connection to your db mysql -u <userName> -p ,run show databases; pick your database using use <databaseName>; and then run show tables; and expect to have list of tables. Second: Perform simple count action on primary key / main table on sql and count instances:


1 Answers

To get a list of all databases without tables in MySQL:

use information_schema

select schema_name from `schemata` s
  left join `tables` t on s.schema_name = t.table_schema
  where t.table_name is null
;

Cheers, Christian

like image 179
Christian Schäfer-Neth Avatar answered Oct 05 '22 08:10

Christian Schäfer-Neth