JDBC: 无法为我的 table 获取复合索引

JDBC: Unable to get composite index for my table

我正在尝试列出我数据库中所有 table 的所有索引(使用不特定于 RDBMS 的通用解决方案)

为了测试我的解决方案,我在 MS SQL 服务器数据库 tempdb

中有 2 个 table

每个 table 都有一个主键,staffs table 也有一个名为 email 的唯一键列。 stores table 上 store_id, store_name.

上有一个复合索引

以下是索引(来自 DBeaver 的屏幕截图)

代码片段如下

.
.
.
System.out.println("**********************************************************");
System.out.println("schema >>> "+schema);
System.out.println("table >>> "+table);
ResultSet indexValues = tableMeta.getIndexInfo(null, null, table, true, false);
while (indexValues.next()) {
    ResultSetMetaData rsmd = indexValues.getMetaData();  
    String idxName = indexValues.getString("INDEX_NAME");
    System.out.println("=====================================================");
    System.out.println("rs1 >>> "+indexValues.getString(1));
    System.out.println("rs2 >>> "+indexValues.getString(2));
    System.out.println("rs3 >>> "+indexValues.getString(3));
    System.out.println("rs4 >>> "+indexValues.getString(4));
    System.out.println("rs5 >>> "+indexValues.getString(5));
    System.out.println("rs6 >>> "+indexValues.getString(6));
    System.out.println("rsmd >>> "+rsmd.getColumnCount());
    System.out.println("INDEX_NAME >>> "+indexValues.getString("INDEX_NAME"));
    System.out.println("INDEX_QUALIFIER >>> "+indexValues.getString("INDEX_QUALIFIER"));
    System.out.println("NON_UNIQUE >>> "+indexValues.getBoolean("NON_UNIQUE"));
    System.out.println("TYPE >>> "+indexValues.getShort("TYPE"));
    System.out.println("ORDINAL_POSITION >>> "+indexValues.getString("ORDINAL_POSITION"));
    System.out.println("COLUMN_NAME >>> "+indexValues.getString("COLUMN_NAME"));
    System.out.println("ASC_OR_DESC >>> "+indexValues.getString("ASC_OR_DESC"));
    System.out.println("FILTER_CONDITION >>> "+indexValues.getString("FILTER_CONDITION"));
    System.out.println("TABLE_SCHEM >>> "+indexValues.getString("TABLE_SCHEM"));
    System.out.println("TABLE_CAT >>> "+indexValues.getString("TABLE_CAT"));
    System.out.println("=====================================================");

}

输出如下(不返回compound/composite索引)

**********************************************************
schema >>> dbo
table >>> stores
=====================================================
rs1 >>> tempdb
rs2 >>> dbo
rs3 >>> stores
rs4 >>> null
rs5 >>> null
rs6 >>> null
rsmd >>> 13
INDEX_NAME >>> null
INDEX_QUALIFIER >>> null
NON_UNIQUE >>> false
TYPE >>> 0
ORDINAL_POSITION >>> null
COLUMN_NAME >>> null
ASC_OR_DESC >>> null
FILTER_CONDITION >>> null
TABLE_SCHEM >>> dbo
TABLE_CAT >>> tempdb
=====================================================
=====================================================
rs1 >>> tempdb
rs2 >>> dbo
rs3 >>> stores
rs4 >>> 0
rs5 >>> stores
rs6 >>> PK__stores__A2F2A30C0F20F60B
rsmd >>> 13
INDEX_NAME >>> PK__stores__A2F2A30C0F20F60B
INDEX_QUALIFIER >>> stores
NON_UNIQUE >>> false
TYPE >>> 1
ORDINAL_POSITION >>> 1
COLUMN_NAME >>> store_id
ASC_OR_DESC >>> A
FILTER_CONDITION >>> null
TABLE_SCHEM >>> dbo
TABLE_CAT >>> tempdb
=====================================================
**********************************************************
schema >>> dbo
table >>> staffs
=====================================================
rs1 >>> tempdb
rs2 >>> dbo
rs3 >>> staffs
rs4 >>> null
rs5 >>> null
rs6 >>> null
rsmd >>> 13
INDEX_NAME >>> null
INDEX_QUALIFIER >>> null
NON_UNIQUE >>> false
TYPE >>> 0
ORDINAL_POSITION >>> null
COLUMN_NAME >>> null
ASC_OR_DESC >>> null
FILTER_CONDITION >>> null
TABLE_SCHEM >>> dbo
TABLE_CAT >>> tempdb
=====================================================
=====================================================
rs1 >>> tempdb
rs2 >>> dbo
rs3 >>> staffs
rs4 >>> 0
rs5 >>> staffs
rs6 >>> PK__staffs__1963DD9CCB589A48
rsmd >>> 13
INDEX_NAME >>> PK__staffs__1963DD9CCB589A48
INDEX_QUALIFIER >>> staffs
NON_UNIQUE >>> false
TYPE >>> 1
ORDINAL_POSITION >>> 1
COLUMN_NAME >>> staff_id
ASC_OR_DESC >>> A
FILTER_CONDITION >>> null
TABLE_SCHEM >>> dbo
TABLE_CAT >>> tempdb
=====================================================
=====================================================
rs1 >>> tempdb
rs2 >>> dbo
rs3 >>> staffs
rs4 >>> 0
rs5 >>> staffs
rs6 >>> UQ__staffs__AB6E6164E6F8EDB6
rsmd >>> 13
INDEX_NAME >>> UQ__staffs__AB6E6164E6F8EDB6
INDEX_QUALIFIER >>> staffs
NON_UNIQUE >>> false
TYPE >>> 3
ORDINAL_POSITION >>> 1
COLUMN_NAME >>> email
ASC_OR_DESC >>> A
FILTER_CONDITION >>> null
TABLE_SCHEM >>> dbo
TABLE_CAT >>> tempdb
=====================================================

我已通读以下文档link [JDBC doc]

此外,通读关于同一主题的这些 Whosebug 问题

[Question 1] [Question 2] [Question 3]

我应该怎么做才能通过 JDBC 完成此操作?我的一个选择是使用特定于数据库的查询来获取索引列表并通过 JDBC 运行 它们(但我不想走那条路)

getIndexInfo()的第四个参数是

unique - when true, return only indices for unique values; when false, return indices regardless of whether unique or not

当您为该参数传递 true 时,只有唯一索引被 returned,但 myCompound1 未定义为唯一,因此 getIndexInfo() 不会 return 它。您需要为该参数添加 false

tableMeta.getIndexInfo(null, null, table, false, false);