How can we get table indexes and related columns with sysindexes table in SQL Server?
I w开发者_如何学JAVAant to get defined indexes and related columns in my tables. I can do this by
sp_helpindex 'tableName'
but I want to get unique and non-unique indexes seperatly. How can we do this by writting a query on sysindexes table?
The sys.indexes
view has a column is_unique
:
select i.name as IndexName
, ic.key_ordinal as IndexColumnPosition
, c.name as IndexColumnName
from sys.indexes i
left join
sys.index_columns ic
on ic.object_id = i.object_id
and ic.index_id = i.index_id
left join
sys.columns c
on c.object_id = ic.object_id
and c.column_id = ic.column_id
where i.object_id = object_id('YourTable')
and i.is_unique = 1
精彩评论