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
AI Thread 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top