2016-01-27 21 views
2

所以,我一直在通過互聯網搜索,似乎無法找到這個解決方案...如何列出所有與它在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表...

有人可以幫助我這個問題......?

回答

1

你這裏的問題是,所引用的表中有一個是由多個列的PRIMARY KEY

PRIMARY KEY(kode_credential, kode_privilege, kode_feature, kode_app) 

這4列連接到其他表,然後你會得到一個笛卡爾乘積:4行爲每個外鍵中的每一列。您需要使用ordinal_position(您的案例中引用表中的約束列 - access)和position_in_unique_constraint(引用表中的約束列 - cred_access),兩者均在key_column_usage視圖中。爲了退出這個功能,你需要一個自聯接,如下所示:

SELECT k1.table_schema, 
     k1.table_name, 
     k1.column_name, 
     k2.table_schema AS referenced_table_schema, 
     k2.table_name AS referenced_table_name, 
     k2.column_name AS referenced_column_name 
FROM information_schema.key_column_usage k1 
JOIN information_schema.referential_constraints fk USING (constraint_schema, constraint_name) 
JOIN information_schema.key_column_usage k2 
    ON k2.constraint_schema = fk.unique_constraint_schema 
AND k2.constraint_name = fk.unique_constraint_name 
AND k2.ordinal_position = k1.position_in_unique_constraint; 

這是相當「神奇」是,MySQL可以做到這一點沒有具體加入像上面。讓你想知道還有什麼其他的兜帽裏的東西可以繼續......

+0

雖然有個問題,當我使用'select * from pg_constraint where contype ='f';'我得到了一個名爲'conkey'的列值''{2,3,4,5}''和一個名爲confkey的值爲'{1,2,3,4}'的列「,這些'position_in_unique_constraint'確實表示confkey的權利?無論哪種方式,它的作品就像魅力!謝謝:D – Raqasyinov

+0

是的,正確的。 'information_schema'視圖是建立在系統目錄上的,它使用這個表'pg_constraint'來填充視圖,這兩個數組對應'ordinal_position'和'position_in_unique_constraint'。 – Patrick

+0

我明白了......基本上'information_schema'中的大多數表都基於'pg_catalog'表......你能給我一些參考資料/書籍來研究'information_schema'和'pg_catalog'之間的關係嗎?非常感謝你提前:) – Raqasyinov

相關問題