SQL to check if database is empty (no tables)

前端 未结 11 1332
后悔当初
后悔当初 2021-02-14 04:59

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!

相关标签:
11条回答
  • 2021-02-14 05:25

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

    0 讨论(0)
  • 2021-02-14 05:25

    In Oracle: select Count(*) from user_tables

    0 讨论(0)
  • 2021-02-14 05:26

    In MYSQL:

    use DATABASE;
    show tables;
    
    0 讨论(0)
  • 2021-02-14 05:26

    SQLServer implementation:

    USE database_name
    SELECT COUNT(*) from information_schema.tables 
    WHERE table_type = 'base table' 
    
    0 讨论(0)
  • 2021-02-14 05:28

    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'
    
    0 讨论(0)
  • 2021-02-14 05:29

    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

    0 讨论(0)
提交回复
热议问题