Postgresql 查詢表引用或被引用的外鍵操作
今天更新兩個(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)
示例:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PostgreSQL如何查詢表大小(單獨(dú)查詢和批量查詢)
PostgreSQL提供了多個(gè)系統(tǒng)管理函數(shù)來查看表,索引表空間及數(shù)據(jù)庫的大小,這篇文章主要給大家介紹了關(guān)于PostgreSQL如何查詢表大小的相關(guān)資料,文中介紹的方法包括單獨(dú)查詢和批量查詢,需要的朋友可以參考下2024-02-02
PostgreSQL 分頁查詢時(shí)間的2種比較方法小結(jié)
這篇文章主要介紹了PostgreSQL 分頁查詢時(shí)間的2種比較方法小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
PostgreSQL的upsert實(shí)例操作(insert on conflict do)
這篇文章主要介紹了PostgreSQL的upsert實(shí)例操作(insert on conflict do),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL使用MySQL作為外部表(mysql_fdw)
PostgreSQL 提供了一種訪問和操作外部數(shù)據(jù)源的機(jī)制,稱為外部數(shù)據(jù)包裝器,本文主要給大家介紹了PostgreSQL使用MySQL作為外部表的方法,感興趣的朋友跟隨小編一起看看吧2022-11-11
PostgreSQL并行計(jì)算算法及參數(shù)強(qiáng)制并行度設(shè)置方法
這篇文章主要介紹了PostgreSQL 并行計(jì)算算法,參數(shù),強(qiáng)制并行度設(shè)置,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
在 PostgreSQL中解決圖片二進(jìn)制數(shù)據(jù)由于bytea_output參數(shù)問題導(dǎo)致顯示不正常的問題
無論 bytea_output 參數(shù)設(shè)置為 hex 還是 escape,你都可以通過 C# 訪問 PostgreSQL 數(shù)據(jù)庫,并且正常獲取并顯示圖片,本篇隨筆介紹這個(gè)問題的處理過程,感興趣的朋友跟隨小編一起看看吧2024-03-03

