1

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

为了测试我的解决方案,我在 MS SQL Server DB 中有 2 个表tempdb

  • 专卖店
  • 员工

每个表都有一个 PK,并且该staffs表还有一个名为 的唯一键列emailstores上的表上有一个复合索引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("=====================================================");

}

输出如下(不返回复合/复合索引)

**********************************************************
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
=====================================================

我已阅读以下文档链接 [JDBC doc]

此外,请阅读有关同一主题的这些 stackoverflow 问题

[问题 1] [问题 2] [问题 3]

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

4

1 回答 1

2

第四个参数getIndexInfo()

unique- 如果为真,则仅返回唯一值的索引;当为 false 时,无论是否唯一,都返回索引

当您传递true该参数时,仅返回唯一索引,但myCompound1未定义为唯一索引,因此 getIndexInfo() 不会返回它。您需要false为该参数粘贴:

tableMeta.getIndexInfo(null, null, table, false , false);
于 2019-11-21T08:43:50.700 回答