0

According to official documentation of sys.indexes, type = 2 -->Nonclustered rowstore (B-tree). But the line 4 of the following example taken from online here is showing type = 2 -->Nonclustered unique. Question: Does type =2 implies Nonclustered unique index. Why the author is using unique in line 4 below?

select i.[name] as index_name,
    substring(column_names, 1, len(column_names)-1) as [columns],
    case when i.[type] = 1 then 'Clustered index'
        when i.[type] = 2 then 'Nonclustered unique index'
        when i.[type] = 3 then 'XML index'
        when i.[type] = 4 then 'Spatial index'
        when i.[type] = 5 then 'Clustered columnstore index'
        when i.[type] = 6 then 'Nonclustered columnstore index'
        when i.[type] = 7 then 'Nonclustered hash index'
        end as index_type,
    case when i.is_unique = 1 then 'Unique'
        else 'Not unique' end as [unique],
    schema_name(t.schema_id) + '.' + t.[name] as table_view, 
    case when t.[type] = 'U' then 'Table'
        when t.[type] = 'V' then 'View'
        end as [object_type]
from sys.objects t
    inner join sys.indexes i
        on t.object_id = i.object_id
    cross apply (select col.[name] + ', '
                    from sys.index_columns ic
                        inner join sys.columns col
                            on ic.object_id = col.object_id
                            and ic.column_id = col.column_id
                    where ic.object_id = t.object_id
                        and ic.index_id = i.index_id
                            order by key_ordinal
                            for xml path ('') ) D (column_names)
where t.is_ms_shipped <> 1
and index_id > 0
order by i.[name]
1

2 Answers 2

3

Does type =2 implies Nonclustered unique index. Why the author is using unique in line 4 below?

The excerpt below from the sys.indexes documentation shows type 2 is a non-clustered rowstore index. Type alone does not indicate whether the index is unique or non-unique. The separate is_unique column specifies if the index is unique.

Type of index:

0 = Heap 1 = Clustered rowstore (B-tree) 2 = Nonclustered rowstore (B-tree) 3 = XML 4 = Spatial 5 = Clustered columnstore index. Applies to: SQL Server 2014 (12.x) and later. 6 = Nonclustered columnstore index. Applies to: SQL Server 2012 (11.x) and later. 7 = Nonclustered hash index. Applies to: SQL Server 2014 (12.x) and later.

I cannot say why the author specified unique in the description for type 2. My guess it's a typo.

0

As far as I know, the table sys.indexes has a different column that is called is_unique and only according to it's value you can know if it is a unique index or none unique index. The person that wrote the query also knows it, because at the 11th line he/she also checks for the value of is_unique column in order to specify this property. My guess is that he/she made a mistake at the string when they wrote 'Nonclustered unique index'. The fact that they don't have another value for noclustered none unique index can also imply that it was a mistake.

Adi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.