我使用以下查询来获取表格上的索引列及其数据类型:
SELECT DISTINCT COL.COLUMN_NAME, COL.DATA_TYPE
FROM DBA_IND_COLUMNS IND
INNER JOIN DBA_TAB_COLUMNS COL
ON ( IND.TABLE_OWNER = COL.OWNER AND IND.TABLE_NAME = COL.TABLE_NAME
AND IND.COLUMN_NAME = COL.COLUMN_NAME)
WHERE IND.TABLE_NAME = 'MY_TABLE' AND TABLE_OWNER = 'SCHEMA'
但是,如何只为一个索引获取列,而不是所有索引的列?
例如: 如果表有索引:
INDEX1:column_a,column_b
INDEX2:column_c,column_d
我当前的查询将导致:
column_a, varchar
column_b, varchar
column_c, varchar
column_d, varchar
但我希望它只能产生:
column_a, varchar
column_b, varchar
答案 0 :(得分:1)
如果您需要表的第一个索引中的列,请尝试使用子查询进行过滤:
SELECT DISTINCT COL.COLUMN_NAME, COL.DATA_TYPE
FROM DBA_IND_COLUMNS IND
INNER JOIN DBA_TAB_COLUMNS COL
ON ( IND.TABLE_OWNER = COL.OWNER AND IND.TABLE_NAME = COL.TABLE_NAME
AND IND.COLUMN_NAME = COL.COLUMN_NAME)
WHERE IND.TABLE_NAME = 'MY_TABLE' AND TABLE_OWNER = 'SCHEMA'
AND INDEX_NAME = (SELECT MIN(INDEX_NAME)
FROM DBA_IND_COLUMNS IND2
WHERE IND2.TABLE_NAME = IND.TABLE_NAME
AND IND2.TABLE_OWNER = IND.TABLE_OWNER)
这甚至可以用一些分析功能重写。
答案 1 :(得分:1)
由于您只想要第一个索引的列(按字母顺序排列),您可以尝试:
SELECT DISTINCT COL.COLUMN_NAME, COL.DATA_TYPE
FROM DBA_IND_COLUMNS IND
INNER JOIN DBA_TAB_COLUMNS COL
ON ( IND.TABLE_OWNER = COL.OWNER AND IND.TABLE_NAME = COL.TABLE_NAME
AND IND.COLUMN_NAME = COL.COLUMN_NAME)
WHERE IND.TABLE_NAME = 'MY_TABLE' AND TABLE_OWNER = 'SCHEMA'
AND INDEX_NAME =
(
SELECT MIN(INDEX_NAME)
FROM DBA_IND_COLUMNS IIND
INNER JOIN DBA_TAB_COLUMNS CCOL
ON ( IIND.TABLE_OWNER = CCOL.OWNER AND IIND.TABLE_NAME = CCOL.TABLE_NAME )
)