【问题标题】:How to get the total number of tables in postgresql?如何获取postgresql中的表总数?
【发布时间】:2012-12-05 13:43:47
【问题描述】:

有什么方法可以获取 Postgresql 数据库中的表总数?我使用的 postgresql 版本是 PostgreSQL 8.4.14。

【问题讨论】:

  • 要探索仅在一个数据库中发生的事情,我通常使用\d。有了这个,您还可以列出表格、视图和序列的总数。

标签: database postgresql postgresql-8.4


【解决方案1】:

如果您只想获取表的数量(没有视图),那么:

select count(*) from information_schema.tables where table_type = 'BASE TABLE';

或者您也可以通过添加 where 条件来按架构名称过滤:

table_schema = 'public'

【讨论】:

    【解决方案2】:
    select Count(*) from sys.tables
    

    【讨论】:

    • 这应该适用于 SQL Server,对吧?不是 PostgreSQL。
    【解决方案3】:
    select count(*)
    from information_schema.tables;
    

    或者,如果您只想查找特定模式的表数:

    select count(*)
    from information_schema.tables
    where table_schema = 'public';
    

    【讨论】:

    • 如果我们要统计桌子的数量怎么办?我知道我们可以看到这个数字,但我想在 sql 语句中使用这个数字?
    【解决方案4】:

    只需尝试在 pg_stat... 表或 information_schema 中搜索,您就可以找到有关您的数据库的非常有用的信息。
    示例:

    select * from  pg_stat_user_tables ;
    select count(*) from  pg_stat_user_tables ; 
    select * from  pg_stat_all_tables ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-10
      • 2014-06-05
      • 2020-11-15
      • 2013-07-21
      • 2014-09-16
      • 1970-01-01
      • 2017-07-09
      • 2016-11-25
      相关资源
      最近更新 更多