Postgresql - 查询表引用或被引用的外键

今天更新两个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)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值