What Are the Primary Keys for a Table? JDBC

The primary key(PK) of a relational table uniquely identifies each row (or record) in the table. DatabaseMetaData provides the getPrimaryKeys() method, which retrieves a description of the given table’s primary key columns:

ResultSet getPrimaryKeys(String catalog, String schema, String table)

When invoking the DatabaseMetaData.getPrimaryKeys() method, be sure to pass catalog and schema values (if they are known); if you pass null values for the first two parameters, then it might take too long to return the result. Database applications that involve the dynamic insertion of records need to know which columns form the primary key. In this way, the application can control the values sent for the insertion of new records. Again, this can be useful when you build database adapters. The solution is as follows:

Consider the following table in a MySQL database:

mysql> describe TestTable77;


In order to find the Primary Key column names for the TestTable77 table, we need to write the following code segment:

// print the list of PKs

The output will be:

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

JDBC Topics