Use of Unique Index in TSQL

SRIVASTAV, SOMYA 0 Reputation points
2024-10-10T07:16:10.43+00:00

In TSQL what is the significance of having Unique constraints with Not Enforced enabled? Even after creating this I am able to insert duplicates within the table

https://learn.microsoft.com/en-us/fabric/data-warehouse/table-constraints

User's image

https://stackoverflow.com/questions/54513136/when-we-should-use-not-enforced-with-foreign-key-constraint-in-db2

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
4,932 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,855 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,640 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,561 Reputation points
    2024-10-10T17:08:47.05+00:00

    setting the NOT ENFORCED option means the data engine does not enforce the constraint. for example, if the unique / primary key constant is NOT ENFORCED, then the engine will not check for duplicates on insert. this can improve insert performance. in the case of foreign key, with NOT ENFORCED the relationship can be inserted / deleted via separate transactions. but the query engine will use the constraint as a hint, and tools can know about the constraint.

    not all sql database support NOT ENFORCED, say SqlServer. in the case of Fabric Warehouse, its the opposite, that is it does not support ENFORCED. with Fabric Warehouse the constraints are just "hints"

    0 comments No comments

  2. LiHongMSFT-4306 27,101 Reputation points
    2024-10-11T02:25:39.1866667+00:00

    Hi @SRIVASTAV, SOMYA

    PRIMARY KEY is only supported when NONCLUSTERED and NOT ENFORCED are both used.

    Are you using Data Warehouse? For Warehouse, PRIMARY KEY and UNIQUE constraint are only supported when NONCLUSTERED and NOT ENFORCED are both used, which means it will not check for the uniqueness of the primary key values.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.