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#
Ollyc's answer is good as it is not Postgres-specific, however, it breaks down when the foreign key references more than one column. The following query works for arbitrary number of columns but it relies heavily on Postgres extensions:
Use the name of the Primary Key to which the Keys are referencing and query the information_schema:
Here 'TABLE_NAME_pkey' is the name of the Primary Key referenced by the Foreign Keys.
I created little tool to query and then compare database schema: Dump PostgreSQL db schema to text
There is info about FK, but ollyc response gives more details.
One another way:
I think what you were looking for and very close to what @ollyc wrote is this:
This will list all the tables that use your specified table as a foreign key
You can do this via the information_schema tables. For example: