【问题标题】:How to find a table having a specific column in postgresql如何在postgresql中查找具有特定列的表
【发布时间】:2013-09-01 17:42:40
【问题描述】:

我使用的是 PostgreSQL 9.1。我有一个表的列名。是否可以找到具有/具有此列的表?如果有,怎么做?

【问题讨论】:

    标签: sql database postgresql postgresql-9.1


    【解决方案1】:

    你也可以这样做

     select table_name from information_schema.columns where column_name = 'your_column_name'
    

    【讨论】:

    • 奇怪的是,我见过这个查询显示@RomanPekar 的查询没有的表的实例;我想知道为什么会这样
    • @KenBellows 我猜 pg_class / pg_attirbute 可以随着 Postgresql 的新版本而改变,而 information_schema 是在 ANSI 规范中定义的。因此,对于一般查询,我会说这个答案更好。例如,有时我需要有对象 ID,在这种情况下,我需要使用 db-engine 特定的表。此外,information_schema 视图始终是数据库引擎特定表的额外步骤,有时会导致(稍微)更差的性能
    • 这是提供的两种解决方案中更准确的一种——就我而言。 pg_class 查询错过了两个(共 150 个)表。 information_schema 查询捕获了所有表。我将不得不四处挖掘,看看为什么两个表落在连接之外。无论如何感谢您的信息!
    【解决方案2】:

    您可以查询system catalogs

    select c.relname
    from pg_class as c
        inner join pg_attribute as a on a.attrelid = c.oid
    where a.attname = <column name> and c.relkind = 'r'
    

    sql fiddle demo

    【讨论】:

    • 请注意,此查询似乎不接受 '%' 通配符,而 Ravi 的答案中的查询可以。
    • @SkippyleGrandGourou 它确实接受“like 'id%'”
    • 无论有没有通配符,这对我都不起作用,我不得不使用 information.schema 进行搜索
    【解决方案3】:

    我使用@Roman Pekar 的查询作为基础并添加了架构名称(与我的情况相关)

    select n.nspname as schema ,c.relname
        from pg_class as c
        inner join pg_attribute as a on a.attrelid = c.oid
        inner join pg_namespace as n on c.relnamespace = n.oid
    where a.attname = 'id_number' and c.relkind = 'r'
    

    sql fiddle demo

    【讨论】:

      【解决方案4】:

      简单地说:

      $ psql mydatabase -c '\d *' | grep -B10 'mycolname'
      

      如果需要,放大 -B 偏移量以获取表名

      【讨论】:

        【解决方案5】:

        通配符支持 查找包含要查找的字符串的表架构和表名。

        select t.table_schema,
               t.table_name
        from information_schema.tables t
        inner join information_schema.columns c on c.table_name = t.table_name
                                        and c.table_schema = t.table_schema
        where c.column_name like '%STRING%'
              and t.table_schema not in ('information_schema', 'pg_catalog')
              and t.table_type = 'BASE TABLE'
        order by t.table_schema;
        

        【讨论】:

          【解决方案6】:
          select t.table_schema,
                 t.table_name
          from information_schema.tables t
          inner join information_schema.columns c on c.table_name = t.table_name 
                                          and c.table_schema = t.table_schema
          where c.column_name = 'name_colum'
                and t.table_schema not in ('information_schema', 'pg_catalog')
                and t.table_type = 'BASE TABLE'
          order by t.table_schema;
          

          【讨论】:

          • edit your answer 包括对您的代码的解释。这个问题已有六年的历史了,除了几个得到好评、解释清楚的答案外,已经有了一个公认的答案。如果您的答案没有这样的解释,它就会被否决或删除。添加该额外信息将有助于证明您的答案在此处继续存在。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-05
          • 2019-02-16
          • 1970-01-01
          • 2015-04-03
          • 1970-01-01
          相关资源
          最近更新 更多