所以,我一直在通過互聯網搜索,似乎無法找到這個解決方案...如何列出所有與它在postgresql中的引用像mysql中的列?
我試圖列出所有與它在postgresql中的參考列。在MySQL中,查詢是這樣的:
select table_schema, table_name, column_name, referenced_table_schema, referenced_table_name, referenced_column_name from informatioN_schema.key_column_usage where table_schema = '(Table Schema)';
,其結果是:
setandlog | access | kode_privilege | NULL | NULL | NULL
setandlog | access | kode_feature | NULL | NULL | NULL
setandlog | access | kode_app | NULL | NULL | NULL
setandlog | access | kode_credential | NULL | NULL | NULL
setandlog | access | username | NULL | NULL | NULL
setandlog | access | kode_credential | setandlog | cred_access | kode_credential
setandlog | access | kode_privilege | setandlog | cred_access | kode_privilege
setandlog | access | kode_feature | setandlog | cred_access | kode_feature
setandlog | access | kode_app | setandlog | cred_access | kode_app
setandlog | access | username | setandlog | login | username
但是當IM使用此查詢在PostgreSQL中嘗試它:
select r.table_schema as table_schema, r.table_name as table_name, r.column_name as column_name, u.table_schema as referenced_table_schema, u.table_name as referenced_table_name, u.column_name as referenced_column_name
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE as u
inner join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS as FK
on U.CONSTRAINT_CATALOG = FK.UNIQUE_CONSTRAINT_CATALOG
and U.CONSTRAINT_SCHEMA = FK.UNIQUE_CONSTRAINT_SCHEMA
and U.CONSTRAINT_NAME = FK.UNIQUE_CONSTRAINT_NAME
inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE as R
ON R.CONSTRAINT_CATALOG = FK.CONSTRAINT_CATALOG
AND R.CONSTRAINT_SCHEMA = FK.CONSTRAINT_SCHEMA
AND R.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
結果不正如我在MySQL查詢中所期望的那樣... 結果如下:
setandlog | access | kode_credential | setandlog | cred_access | kode_credential
setandlog | access | kode_privilege | setandlog | cred_access | kode_credential
setandlog | access | kode_feature | setandlog | cred_access | kode_credential
setandlog | access | kode_app | setandlog | cred_access | kode_credential
setandlog | access | kode_credential | setandlog | cred_access | kode_privilege
setandlog | access | kode_privilege | setandlog | cred_access | kode_privilege
setandlog | access | kode_feature | setandlog | cred_access | kode_privilege
setandlog | access | kode_app | setandlog | cred_access | kode_privilege
setandlog | access | kode_credential | setandlog | cred_access | kode_feature
setandlog | access | kode_privilege | setandlog | cred_access | kode_feature
setandlog | access | kode_feature | setandlog | cred_access | kode_feature
setandlog | access | kode_app | setandlog | cred_access | kode_feature
setandlog | access | kode_credential | setandlog | cred_access | kode_app
setandlog | access | kode_privilege | setandlog | cred_access | kode_app
setandlog | access | kode_feature | setandlog | cred_access | kode_app
setandlog | access | kode_app | setandlog | cred_access | kode_app
setandlog | access | username | setandlog | login | username
好像有很多redudant數據... 當我檢查查詢,似乎是在UNIQUE_CONSTRAINT_NAME
沒有diferrence在INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
表...
有人可以幫助我這個問題......?
雖然有個問題,當我使用'select * from pg_constraint where contype ='f';'我得到了一個名爲'conkey'的列值''{2,3,4,5}''和一個名爲confkey的值爲'{1,2,3,4}'的列「,這些'position_in_unique_constraint'確實表示confkey的權利?無論哪種方式,它的作品就像魅力!謝謝:D – Raqasyinov
是的,正確的。 'information_schema'視圖是建立在系統目錄上的,它使用這個表'pg_constraint'來填充視圖,這兩個數組對應'ordinal_position'和'position_in_unique_constraint'。 – Patrick
我明白了......基本上'information_schema'中的大多數表都基於'pg_catalog'表......你能給我一些參考資料/書籍來研究'information_schema'和'pg_catalog'之間的關係嗎?非常感謝你提前:) – Raqasyinov