Search code examples
mysqljdbcforeign-keysunique-constraintnotnull

How to get the column name of the CANDIDATE Keys through jdbc?


I need to find candidate keys of a table. As for a column to be a candidate key, it must be (1) NOT NULL and (2) UNIQUE. So what I am doing is:

   for(i=1;i<colcount; i++){
       if( (resultset.getmetadata().isnullable(i)==0) && (UNIQUE_COND) ){
            // IS A CANDIDATE KEY
       }
   }

How can I check for UNIQUE condition? I am using mysql db through JDBC. Is there another way to get candidate keys? Thanks.


Solution

  • You need to use the DatabaseMetaData, not the metadata of the resultset. Specifically you need to look at: