我正在尝试列出我的数据库中所有表的所有索引(使用不特定于 RDBMS 的通用解决方案)
为了测试我的解决方案,我在 MS SQL Server DB 中有 2 个表tempdb
- 专卖店
- 员工
每个表都有一个 PK,并且该staffs
表还有一个名为 的唯一键列email
。stores
上的表上有一个复合索引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 问题
我应该怎么做才能通过 JDBC 完成这项工作?我有一个选择是让数据库特定查询来获取索引列表并通过 JDBC 运行它们(但我不想走那条路)