Script to display index uniqueness

  • I am from the Oracle world and use the following script to display unique and nonunique indexes

    select index_name, table_name, uniqueness

    from user_indexes

    How do I display this information in SQL Server? 

    Thanks.

     

  • This would be much easier if there were INFORMATION_SCHEMA views for indexes.  Instead, you need to go some system table work:

    Try something like:

    select object_name(id) as TableName

         , name as IndexName

         , indexproperty(id, name, 'IsUnique') as IsUnique

    from  sysindexes

    where objectproperty(id, 'IsMSShipped') = 0

      and indexproperty(id, name, 'IsStatistics') = 0

      and indid not in (0,255)

    order by TableName

    The third column has value 1 if unique, 0 if not unique.  The where clause filters out system objects (leaving user objects), statistics -- which show up in sysindexes are are not truly indexes -- and non-clustered table and text entries.

    Hope this helps,

    Scott Thornburg

     

  • Hi Scott,

    Thanks.  Your script returned the information I needed.

    Again thanks.

     

     

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply