這篇文章主要介紹了Postgresql 查詢表引用或被引用的外鍵操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
今天更新兩個(gè)SQL。是用來查詢PG中,主表被子表引用的外鍵,或子表引用了哪個(gè)主表的主鍵。
廢話不多說,直接上實(shí)驗(yàn)!
CentOS 7 + PG 10
創(chuàng)建兩個(gè)實(shí)驗(yàn)表,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
插入數(shù)據(jù)
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".
查詢主表被哪個(gè)子表引用。如果結(jié)果為空,說明沒有任何子表引用的該表。
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)
查詢子表引用的哪個(gè)主表。如果結(jié)果為空,說明沒有任何引用主表。
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)
補(bǔ)充:PostgreSQL 外鍵引用查詢
根據(jù)一個(gè)表名,查詢所有外鍵引用它的表,以及那些外鍵的列名
key_column_usage(系統(tǒng)列信息表),
pg_constraint(系統(tǒng)所有約束表)
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)
示例:
文章來源:腳本之家
來源地址:https://www.jb51.net/article/205219.htm
申請創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!