Unique_index_id Value in sys.key_constraints & is_unique Column in sys.indexes

  • Thread starter Thread starter Pepper Mint
  • Start date Start date
  • Tags Tags
    Column Value
Click For Summary

Discussion Overview

The discussion revolves around the relationship between the unique_index_id in sys.key_constraints and the is_unique column in sys.indexes within SQL Server. Participants explore whether the unique_index_id corresponds to the index_id when is_unique is true, and they examine the implications of this relationship on database design and metadata.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant seeks confirmation that the unique_index_id in sys.key_constraints matches the index_id in sys.indexes when is_unique is true.
  • Another participant references MSDN documentation to support the claim that the unique_index_id corresponds to the unique index created to enforce the constraint.
  • A participant shares a SQL query that yields sensible results, suggesting that the relationship holds under certain conditions.
  • Concerns are raised about the adequacy of the provided SQL query to cover all database design scenarios, indicating potential limitations in its application.
  • There is a suggestion that the index metadata may be redundant or not normalized, prompting further inquiry into the nature of the constraint information.
  • Another participant notes that constraint information can still be accessed without relying on sys.key_constraints.

Areas of Agreement / Disagreement

Participants express varying degrees of agreement regarding the relationship between unique_index_id and index_id, with some supporting the correlation while others question its completeness across different database designs. The discussion remains unresolved regarding the implications of redundancy in index metadata.

Contextual Notes

Participants highlight potential limitations in the SQL query's ability to account for diverse database designs, indicating that the relationship may not be universally applicable.

Pepper Mint
Messages
91
Reaction score
139
I find in SQL Server, sys.indexes contains an is_unique column and sys.key_constraints contains unique_index_id. Could you help confirm me that the unique_index_id value in the latter is also the index_id value of the former when its is_unique becomes true or 1 ?

Thank you.
 
Technology news on Phys.org
Seems so, according to MSDN:https://technet.microsoft.com/en-us/library/ms174321(v=sql.110).aspx
ID of the corresponding unique index in the parent object that was created to enforce this constraint.

This gives me sensible results:

Code:
select      Object_Name(I.object_id) ObjectName, I.name IName, C.name ConstraintName
    ,       I.type_desc IndexType, C.type_desc ConstraintType
from        sys.indexes I
left join   sys.key_constraints C
    on      I.object_id = C.parent_object_id
    and     i.index_id = C.unique_index_id
where       I.is_unique = 1
    and     not c.object_id is null

(Slightly odd SQL as I was running it in bits (not always all of it at once) to see effects.)
 
  • Like
Likes   Reactions: Pepper Mint
sdkfz said:
Thanks! I just feel something redundant in SQL Server.
This gives me sensible results:

Code:
select      Object_Name(I.object_id) ObjectName, I.name IName, C.name ConstraintName
    ,       I.type_desc IndexType, C.type_desc ConstraintType
from        sys.indexes I
left join   sys.key_constraints C
    on      I.object_id = C.parent_object_id
    and     i.index_id = C.unique_index_id
where       I.is_unique = 1
    and     not c.object_id is null
(Slightly odd SQL as I was running it in bits (not always all of it at once) to see effects.)

uhmm..., despite of the bits you are doing, I think this query is generally correct but not enough to cover all cases of various DB designs.
 
Pepper Mint said:
Thanks! I just feel something redundant in SQL Server.uhmm..., despite of the bits you are doing, I think this query is generally correct but not enough to cover all cases of various DB designs.

Not sure what you mean; certainly I was just looking at the specs of your own question.
 
  • Like
Likes   Reactions: Pepper Mint
Do you mean index metadata is redundant or maybe not normalized?
 
jim mcnamara said:
Do you mean index metadata is redundant or maybe not normalized?
I only find constraint information is still obtainable even without sys.key_constraints.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K