Is there a way using SQL to list all foreign keys for a given table? I know the table name / schema and I can plug that in.
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
Extension to ollyc recipe :
Then:
SELECT * FROM foreign_keys_view WHERE table_name='YourTableNameHere'
;None of the existing answers gave me results in the form that I actually wanted them in. So here is my (gargantuan) query for finding information about foreign keys.
A few notes:
from_cols
andto_cols
could be vastly simplified on Postgres 9.4 and later usingWITH ORDINALITY
rather than the window-function-using hackery I'm using.UNNEST
. I don't think it will, but I don't have any multiple-column foreign keys in my dataset to test with. Adding the 9.4 niceties eliminates this possibility altogether.ORDER BY
in aggregate functions)STRING_AGG
withARRAY_AGG
if you want an array of columns rather than a comma-separated string.-
I wrote a solution that like and use frequently. The code is at http://code.google.com/p/pgutils/. See the pgutils.foreign_keys view.
Unfortunately, the output is too wordy to include here. However, you can try it on a public version of the database here, like this:
This works with 8.3 at least. I anticipate updating it, if needed, in the next few months.
-Reece
You can use the PostgreSQL system catalogs. Maybe you can query pg_constraint to ask for foreign keys. You can also use the Information Schema
This query works correct with composite keys also:
check the ff post for your solution and don't forget to mark this when you fine this helpful
http://errorbank.blogspot.com/2011/03/list-all-foreign-keys-references-for.html