2017-02-20 46 views
0

如何使用postgresql 9.5中描述的格式檢索主鍵,外鍵和唯一鍵以及特定模式的相應列名?我是初學者,請幫助...如何在postgresql 9.5中檢索所有密鑰及其列名和特定模式的表名?

Table_name|Primary_key_name|pk_column_name|foreign_key_name|fk_colum_name|unique_key_name|uk_column_name 
+0

可以有更而不是一個唯一的或外鍵,並且約束不一定用一列來定義。您將從['information_schema'](https://www.postgresql.org/docs/current/static/information-schema.html)視圖獲取大部分信息,特別是'tables','table_constraints'和' constraint_column_usage'。 –

+0

是的,但我想顯示所有列 – Karthik

回答

0

要查看基本表的詳細信息

SELECT * 
FROM information_schema.columns 
WHERE table_name = 'yourTableName'; 

,列出所有FORIEN KEY,PRIMARY KEY,UNIQUE KEY約束

SELECT tc.constraint_name, 
tc.constraint_type, 
tc.table_name, 
kcu.column_name, 
tc.is_deferrable, 
tc.initially_deferred, 
rc.match_option AS match_type, 

rc.update_rule AS on_update, 
rc.delete_rule AS on_delete, 
ccu.table_name AS references_table, 
ccu.column_name AS references_field 
FROM information_schema.table_constraints tc 

LEFT JOIN information_schema.key_column_usage kcu 
ON tc.constraint_catalog = kcu.constraint_catalog 
AND tc.constraint_schema = kcu.constraint_schema 
AND tc.constraint_name = kcu.constraint_name 

LEFT JOIN information_schema.referential_constraints rc 
ON tc.constraint_catalog = rc.constraint_catalog 
AND tc.constraint_schema = rc.constraint_schema 
AND tc.constraint_name = rc.constraint_name 

LEFT JOIN information_schema.constraint_column_usage ccu 
ON rc.unique_constraint_catalog = ccu.constraint_catalog 
AND rc.unique_constraint_schema = ccu.constraint_schema 
AND rc.unique_constraint_name = ccu.constraint_name 

WHERE lower(tc.constraint_type) in ('foreign key') 
+0

更新,請檢查新的 –

相關問題