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

如何获取Postgresql中特定列中使用的所有表的列表,sql,postgresql,Sql,Postgresql,我有一个表users:id,name,等等 我想获得使用users.id的所有表和视图的列表 例如,如果表user\u new将users.id引用为FK我希望在查询中显示它 如何做到这一点?尝试使用信息模式 有关意见: 从信息\u schema.views中选择表\u schema、表\u name,其中视图\u定义类似“%users.id%” 对于表格: 选择table_schema,table_name from information_schema.columns,其中table_nam

我有一个表
users
id
name
,等等

我想获得使用
users.id的所有表和视图的列表

例如,如果表
user\u new
users.id
引用为
FK
我希望在查询中显示它


如何做到这一点?

尝试使用信息模式

有关意见:

从信息\u schema.views中选择表\u schema、表\u name,其中视图\u定义类似“%users.id%”

对于表格:


选择table_schema,table_name from information_schema.columns,其中table_name='users'和column_name='id'

尝试使用信息\u架构

 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'
有关意见:

从信息\u schema.views中选择表\u schema、表\u name,其中视图\u定义类似“%users.id%”

对于表格:


选择table_schema,table_name from information_schema.columns,其中table_name='users'和column_name='id'

尝试使用信息\u架构

 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'
有关意见:

从信息\u schema.views中选择表\u schema、表\u name,其中视图\u定义类似“%users.id%”

对于表格:


选择table_schema,table_name from information_schema.columns,其中table_name='users'和column_name='id'

尝试使用信息\u架构

 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'
有关意见:

从信息\u schema.views中选择表\u schema、表\u name,其中视图\u定义类似“%users.id%”

对于表格:

选择table_schema,table_name from information_schema.columns,其中table_name='users'和column_name='id'

 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'

依照

pg_约束

目录pg_约束存储检查、主键、唯一、外来项 表上的键和排除约束

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

请参阅
oid
regclass


依照

pg_约束

目录pg_约束存储检查、主键、唯一、外来项 表上的键和排除约束

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

请参阅
oid
regclass


依照

pg_约束

目录pg_约束存储检查、主键、唯一、外来项 表上的键和排除约束

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

请参阅
oid
regclass


依照

pg_约束

目录pg_约束存储检查、主键、唯一、外来项 表上的键和排除约束

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

请参阅
oid
regclass

使用

例如:

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)
上面的查询选择引用
某些表的所有对象。
您可以筛选特定列和/或所需关系类型的结果。
要根据第一列仅选择表和视图,请使用:

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)    
使用

例如:

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)
上面的查询选择引用
某些表的所有对象。
您可以筛选特定列和/或所需关系类型的结果。
要根据第一列仅选择表和视图,请使用:

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)    
使用

例如:

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)
上面的查询选择引用
某些表的所有对象。
您可以筛选特定列和/或所需关系类型的结果。
要根据第一列仅选择表和视图,请使用:

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)    
使用

例如:

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)
上面的查询选择引用
某些表的所有对象。
您可以筛选特定列和/或所需关系类型的结果。
要根据第一列仅选择表和视图,请使用:

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)    

如果这是一次性检查,最简单的方法是运行
BEGIN;ALTERTABLE用户删除列id;回滚
下拉操作应失败,引用约束和视图将被删除