濮阳杆衣贸易有限公司

主頁 > 知識庫 > Postgresql 查詢表引用或被引用的外鍵操作

Postgresql 查詢表引用或被引用的外鍵操作

熱門標簽:天津開發(fā)區(qū)地圖標注app 濟南外呼網絡電話線路 400電話申請客服 移動外呼系統(tǒng)模擬題 江蘇400電話辦理官方 電話機器人怎么換人工座席 廣州電銷機器人公司招聘 地圖標注要花多少錢 電銷機器人能補救房產中介嗎

今天更新兩個SQL。是用來查詢PG中,主表被子表引用的外鍵,或子表引用了哪個主表的主鍵。

廢話不多說,直接上實驗!

CentOS 7 + PG 10

創(chuàng)建兩個實驗表,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(系統(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)

示例:

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • postgresql 索引之 hash的使用詳解
  • PostgreSQL Sequence序列的使用詳解
  • PostgreSQL之INDEX 索引詳解
  • PostgreSql 重建索引的操作
  • PostgreSql 的hash_code函數的用法說明

標簽:榆林 海西 濮陽 寶雞 溫州 辛集 杭州 昭通

巨人網絡通訊聲明:本文標題《Postgresql 查詢表引用或被引用的外鍵操作》,本文關鍵詞  Postgresql,查詢表,引用,或,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Postgresql 查詢表引用或被引用的外鍵操作》相關的同類信息!
  • 本頁收集關于Postgresql 查詢表引用或被引用的外鍵操作的相關信息資訊供網民參考!
  • 推薦文章
    宜君县| 静海县| 胶州市| 天等县| 桑植县| 保康县| 连城县| 蕲春县| 凤城市| 闵行区| 石渠县| 福州市| 永登县| 锡林郭勒盟| 奇台县| 荆州市| 锡林浩特市| 策勒县| 隆回县| 阿坝县| 渭源县| 桐庐县| 荆门市| 台前县| 新田县| 西安市| 德江县| 北辰区| 天峻县| 策勒县| 甘谷县| 南开区| 周口市| 弋阳县| 镇沅| 泽普县| 微山县| 万荣县| 肇州县| 辉南县| 洪江市|