PostgreSQL | 외래키(Forein key) 테이블 및 컬럼 목록 조회
조회하고자 하는 테이블이 외래키로 참조하고 있는 테이블 및 컬럼 목록 조회
SELECT CCU.table_name AS search_table_name
, CCU.column_name AS search_column_name
, KCU.table_name AS foreign_table_name
, KCU.column_name AS foreign_column_name
, KCU.constraint_name AS foreign_constraint_name
FROM information_schema.table_constraints TC
JOIN information_schema.key_column_usage KCU ON TC.constraint_name = KCU.constraint_name
JOIN information_schema.constraint_column_usage CCU ON CCU.constraint_name = TC.constraint_name
WHERE TC.constraint_type = 'FOREIGN KEY'
AND CCU.table_name = #{조회하고자 하는 테이블명};
조회하고자 하는 테이블을 외래키로 참조하고 있는 테이블 및 컬럼 목록 조회
SELECT TC.table_name AS search_table_name
, KCU.column_name AS search_column_name
, CCU.table_name AS foreign_table_name
, CCU.column_name AS foreign_column_name
, CCU.constraint_name AS foreign_constraint_name
FROM information_schema.table_constraints TC
JOIN information_schema.key_column_usage KCU ON KCU.constraint_name = TC.constraint_name
JOIN information_schema.constraint_column_usage CCU ON CCU.constraint_name = TC.constraint_name
WHERE TC.constraint_type = 'FOREIGN KEY'
AND TC.table_name = #{조회하고자 하는 테이블명};
최종 수정 : 2020-11-12