11

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!

1
  • 1
    Presumably, you mean 'no user defined tables or views' since the system catalog will exist. Commented Jun 18, 2009 at 14:37

12 Answers 12

17
SELECT COUNT(DISTINCT `table_name`) FROM `information_schema`.`columns` WHERE `table_schema` = 'your_db_name'

will return the actual number of tables (or views) in your DB. If that number is 0, then there are no tables.

Sign up to request clarification or add additional context in comments.

6 Comments

this will also return the number of views.
But if that number is more than 0? Maybe there are no tables, but there are views?
@AlexKuznetsov Yes, but I don't believe that you can have a view without a table to base it on?
@MiffTheFox: CREATE DATAbASE DbWithNoTablesAndOneView; GO USE DbWithNoTablesAndOneView; GO CREATE VIEW dbo.ViewWihtoutUnderlyingTable As SELECT 1 AS n; GO
A more realistic example: create view wrapped_rand_view as select rand( ) as random_value go create function wrapped_rand() returns float as begin declare @f float set @f = (select random_value from wrapped_rand_view) return @f end mydatabasesupport.com/forums/sqlserver-programming/…
|
14
select count(*)
  from information_schema.tables
 where table_type = 'BASE TABLE'
   and table_schema = 'your_database_name_here'

2 Comments

Hmm, I think there is something wrong with this SQL. I'm using MySQL and getting 0 on a database with tables in it.
It worked well for me: But I hardly found how to call First I wrote result = cmd.ExecuteNonQuery which returns -1 Then after I found result = int.Parse(cmd.ExecuteScalar().ToString()); which returns zero if there is no table.
7

In MYSQL:

use DATABASE;
show tables;

Comments

6

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

1 Comment

This is absolutely the right way to go if you want to get all empty databases. Thanks @Christian!
2

If you're using SQL Server 2005 or greater, you can use one of the system views to acheive this for the current db:

select Count(*)
from sys.tables
where [type] = 'U'

Comments

2

SQLServer implementation:

USE database_name
SELECT COUNT(*) from information_schema.tables 
WHERE table_type = 'base table' 

Comments

0

"select * from information_schema.tables" will give you a list of tables on most databases.

Comments

0

In Oracle: select Count(*) from user_tables

Comments

0

I needed something that would give me an exit code to use in Bash. This builds off of @longneck's solid answer. If the database has tables, the select statement will set the contents column as "has tables". Grep will return a successful 0 in this case, otherwise it will return a non-zero.

#!/bin/bash
user=username
pw=passwd
db=database
mysql -u ${user} -p"${pw}" -D ${db} --execute="SELECT CASE COUNT(*) WHEN '0' THEN 'empty database' ELSE 'has tables' END AS contents FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = '${db}';" | grep 'has tables'
echo $?

Comments

0

In bash:

db_name='my_db'
mysql -s --skip-column-names -e "SELECT COUNT(DISTINCT table_name) FROM information_schema.columns WHERE table_schema = '$db_name'"

Comments

0

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.

+----------------------------------------+
| Tables_in_databaseName                 |
+----------------------------------------+
| databaseA                              |
| databaseB                              |
| databaseC                              |
+----------------------------------------+

Second: Perform simple count action on primary key / main table on sql and count instances:

select count(*) from <primaryKeyColumn>;

count result should be > 0

+----------+
| count(*) |
+----------+
|   100    |
+----------+

Comments

0

For MySQL 8, I had to tweak my query into:

SELECT COUNT(*) from information_schema.tables 
WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA=DATABASE();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.