Gherbi Hicham
Gherbi Hicham

Reputation: 2584

Getting all foreign keys corresponding to a table and the tables that hold those foreign keys with JDBC

I need to get all the foreign keys of a particular table and get the names of the tables containing those foreign keys, is there a simple way to do this using JDBC?

I know there is a way to do this for a particular database using a query, but I need to get a generic solution using JDBC.

Upvotes: 0

Views: 197

Answers (1)

Mark Rotteveel
Mark Rotteveel

Reputation: 108961

You need to use DatabaseMetaData.getExportedKeys:

Retrieves a description of the foreign key columns that reference the given table's primary key columns (the foreign keys exported by a table).

For example

dbmd.getExportedKeys(null, null, "SOME_TABLE_NAME")

will return a list of all tables and their foreign keys pointing to the primary key of SOME_TABLE_NAME.

Upvotes: 1

Related Questions