欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Postgresql 查询表引用或被引用的外键操作

程序员文章站 2022-03-13 22:59:44
今天更新两个sql。是用来查询pg中,主表被子表引用的外键,或子表引用了哪个主表的主键。废话不多说,直接上实验!centos 7 + pg 10创建两个实验表,test01为主表,test02为子表,...

今天更新两个sql。是用来查询pg中,主表被子表引用的外键,或子表引用了哪个主表的主键。

废话不多说,直接上实验!

centos 7 + pg 10

创建两个实验表,test01为主表,test02为子表,test02引用test01中的id列。

test=# create table test01(
test(# id int primary key,
test(# col1 varchar(20)
test(# );
create table
 
test=# create table test02(
test(# id int primary key,
test(# test01_id int references test01(id),
test(# col1 varchar(20)
test(# );
create table

插入数据

test=# insert into test01 values (1, 'a');
insert 0 1
test=# insert into test01 values (2, 'b');
insert 0 1
test=# insert into test01 values (3, 'c');
insert 0 1
test=# insert into test02 values (1, 1, 'a');
insert 0 1
test=# insert into test02 values (2, 1, 'a');
insert 0 1
test=# insert into test02 values (3, 1, 'a');
insert 0 1
test=# insert into test02 values (4, 2, 'b');
insert 0 1
test=# insert into test02 values (5, 2, 'b');
insert 0 1
test=# insert into test02 values (6, 11, 'b');
error: insert or update on table "test02" violates foreign key constraint "test02_test01_id_fkey"
detail: key (test01_id)=(11) is not present in table "test01".

查询主表被哪个子表引用。如果结果为空,说明没有任何子表引用的该表。

test=# select
tc.constraint_name,
tc.table_name, # 子表
kcu.column_name,
ccu.table_name as foreign_table_name, # 主表
ccu.column_name as foreign_column_name,
tc.is_deferrable,
tc.initially_deferred
from
information_schema.table_constraints as tc
join information_schema.key_column_usage as kcu on tc.constraint_name = kcu.constraint_name
join information_schema.constraint_column_usage as ccu on ccu.constraint_name = tc.constraint_name
where constraint_type = 'foreign key' and ccu.table_name='test01'; # 输入主表
constraint_name | table_name | column_name | foreign_table_name | foreign_column_name | is_deferrable | initially_deferred
-----------------------+------------+-------------+--------------------+---------------------+---------------+--------------------
test02_test01_id_fkey | test02 | test01_id | test01 | id | no | no
(1 row)

查询子表引用的哪个主表。如果结果为空,说明没有任何引用主表。

test=# select
tc.constraint_name,
tc.table_name, # 子表
kcu.column_name,
ccu.table_name as foreign_table_name,
ccu.column_name as foreign_column_name, # 主表
tc.is_deferrable,
tc.initially_deferred
from
information_schema.table_constraints as tc
join information_schema.key_column_usage as kcu on tc.constraint_name = kcu.constraint_name
join information_schema.constraint_column_usage as ccu on ccu.constraint_name = tc.constraint_name
where constraint_type = 'foreign key' and tc.table_name='test02'; # 输入子表
constraint_name | table_name | column_name | foreign_table_name | foreign_column_name | is_deferrable | initially_deferred
-----------------------+------------+-------------+--------------------+---------------------+---------------+--------------------
test02_test01_id_fkey | test02 | test01_id | test01 | id | no | no
(1 row)

补充:postgresql 外键引用查询

根据一个表名,查询所有外键引用它的表,以及那些外键的列名

key_column_usage(系统列信息表),

pg_constraint(系统所有约束表)

select x.table_name,
    x.column_name
 from information_schema.key_column_usage x
 inner join (select t.relname, 
            a.conname 
         from pg_constraint a 
         inner join pg_class ft
             on ft.oid = a.confrelid 
         inner join pg_class t 
             on t.oid = a.conrelid
        where a.contype = 'f' 
         and a.confrelid =
            (select e.oid 
             from pg_class e
             where e.relname = 'xxx_table')
        ) tp 
     on (x.table_name = tp.relname and
       x.constraint_name = tp.conname)

示例:

Postgresql 查询表引用或被引用的外键操作

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。