Last active
January 2, 2016 11:09
-
-
Save xivSolutions/8294725 to your computer and use it in GitHub Desktop.
Crude pg SQL to select Foreign Key relationships in Db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
parent.table_name parent, | |
parent.constraint_name key_name, | |
tc.constraint_type key_type, | |
(SELECT c.table_name | |
FROM information_schema.table_constraints c | |
WHERE c.constraint_name = parent.constraint_name) child, | |
(SELECT pid.column_name | |
FROM information_schema.constraint_column_usage pid | |
WHERE pid.constraint_name = parent.constraint_name) pid, | |
(SELECT cid.column_name | |
FROM information_schema.key_column_usage cid | |
WHERE cid.constraint_name = parent.constraint_name) cid | |
FROM information_schema.table_constraints tc | |
INNER JOIN information_schema.constraint_table_usage parent | |
ON parent.constraint_name = tc.constraint_name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment