Postgresql

PostgreSQL - 如何列出表主鍵的所有外鍵連結

  • April 24, 2020

我有一個res_users帶有主鍵的表id

我想列出id我數據庫中所有表的所有外鍵連結。

您可以查詢目錄表:

SELECT la.attrelid::regclass AS referencing_table,
      la.attname AS referencing_column
FROM pg_constraint AS c
  JOIN pg_index AS i
     ON i.indexrelid = c.conindid
  JOIN pg_attribute AS la
     ON la.attrelid = c.conrelid
        AND la.attnum = c.conkey[1]
  JOIN pg_attribute AS ra
     ON ra.attrelid = c.confrelid
        AND ra.attnum = c.confkey[1]
WHERE c.confrelid = 'res_users'::regclass
 AND c.contype = 'f'
 AND ra.attname = 'id'
 AND cardinality(c.confkey) = 1;

引用自:https://dba.stackexchange.com/questions/265732