What Are the Exported Keys for a Table? JDBC

To retrieve a description of the foreign key columns that reference the given table’s primary key columns (the foreign keys exported by a table), you can use the DatabaseMetaData. getExportedKeys() method. This method returns its result as a ResultSet object (which is not very useful for web-based applications). For web-based applications, it is better to return the result as HTML or XML.

The signature of DatabaseMetaData’s .getExportedKeys() is as follows:

This method retrieves a description of the foreign key columns that reference the given table’s primary key columns (the foreign keys exported by a table). They are ordered by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, and KEY_SEQ. Each foreign key column description has the columns shown in Table 9-2.

Table : ResultSet Columns for getExportedKeys()

ResultSet Columns for getExportedKeys()

ResultSet Columns for getExportedKeys()

The parameters are

  • catalog: A catalog name; must match the catalog name as it is stored in this database. "" retrieves those without a catalog; null means that the catalog name should not be used to narrow the search.
  • schema: A schema name; must match the schema name as it is stored in the database. "" retrieves those without a schema; null means that the schema name should not be used to narrow the search.
  • table: A table name; must match the table name as it is stored in this database.

Oracle Database Setup

Let’s create two tables (dept_table and emp_table) and define the PK and FK. Also, keep in mind that that if you violate the PK and FK rules, the SQL INSERT operation will fail.

MySQL Database Setup

Database changed

The Solution

Invoking GetExportedKeys for MySQL

Figure : shows how to run the solution for the MySQL database.

Invoking GetExportedKeys for MySQL

Invoking GetExportedKeys for Oracle

Figure : shows how to run the solution for the Oracle database.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics