如何获取Postgresql中特定列中使用的所有表的列表

时间:2015-09-24 08:01:04

标签: sql postgresql

我有一张表function validateAddresscode(Addr) { var addrRegExp = /^([pP]{1,1}[\.]?[oO]{1,1}[\.]?)$/igm; if (addrRegExp.test(Addr)) { return true; } else { return false; } } usersid等...

我想获得所有表格的列表,使用name

的视图

例如,如果表users.id正在将user_new恢复为users.id我希望在查询中显示它。

我该怎么做?

3 个答案:

答案 0 :(得分:2)

使用pg_depend

示例:

create table some_table (
    id serial primary key);

create table child_table (
    id int, 
    master_id int references some_table(id));

create view some_view as 
    select * from some_table;

select distinct pg_describe_object(classid, objid, objsubid)
from pg_depend 
where refobjid = 'some_table'::regclass

                     pg_describe_object
------------------------------------------------------------
 sequence some_table_id_seq
 constraint some_table_pkey on table some_table
 type some_table
 default for table some_table column id
 constraint child_table_master_id_fkey on table child_table
 rule _RETURN on view some_view
(6 rows)

上述查询选择引用some_table的所有对象。 您可以筛选特定列和/或所需关系类型的结果。 要根据第一列仅选择表和视图,请使用:

select distinct pg_describe_object(classid, objid, objsubid)
from pg_depend
where 
    refobjid = 'some_table'::regclass 
    and refobjsubid = 1 -- only for column #1
    and deptype = 'n'; -- a normal relationship between separately-created objects

                     pg_describe_object
------------------------------------------------------------
 constraint child_table_master_id_fkey on table child_table
 rule _RETURN on view some_view
(2 rows)    

答案 1 :(得分:0)

尝试使用information_schema

观点:

select table_schema,table_name from information_schema.views where view_definition ilike '%users.id%';

表格

select table_schema,table_name from information_schema.columns where table_name='users' and column_name='id';

答案 2 :(得分:0)

 SELECT conrelid::regclass table_name
    FROM pg_constraint c
    WHERE c.confrelid = 'user'::regclass::oid
        AND c.confkey @> (
            SELECT array_agg(attnum)
            FROM pg_attribute
            WHERE attname = 'id'
                AND attrelid = c.confrelid
            )
        AND contype = 'f'

根据PostgreSQL Doc

pg_constraint

  

目录pg_constraint存储检查,主键,唯一,外来   键和表格上的排除约束

     

contype : - c =检查约束, f =外键约束 ,p =主键约束,u =唯一约束,t =约束触发器,   x =排除约束,
   conrelid :此约束所在的表格,
   confrelid :如果是外键,引用表,
   conkey :如果是表约束(包括外键,但不是约束触发器),受约束列的列表

请参阅oid regclass@echo off break>"%~dp0\new.txt" ren *txt *js