How to get the column name of the CANDIDATE Keys through jdbc?
-
06-07-2021 - |
Question
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:
- getPrimaryKeys
- getIndexInfo with parameter
unique
set to true.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow