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
In SQL Server, the sys.indexes table includes an is_unique column, while sys.key_constraints features a unique_index_id that corresponds to the index_id in sys.indexes when is_unique is true. This relationship is confirmed by MSDN documentation, which states that the unique_index_id indicates the ID of the unique index created to enforce the constraint. A query provided demonstrates this relationship effectively, yielding sensible results when filtering for unique indexes. However, there is a discussion about potential redundancy in SQL Server's metadata, with some users suggesting that constraint information can still be accessed without relying on sys.key_constraints, indicating possible normalization issues in the database design.
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 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 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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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