Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Marusyk
Frequent Visitor

UNIQUE constraint doesn't work in Fabric Warehouse

Could anyone suggest why UNIQUE constraint doesn't work in the warehouse? 

I expected that creating PRIMARY KEY will be enough to not all duplication but the duplication is allowed even if I created both

 

 

CREATE TABLE [dev].[Calendar]
(
    [Year] INT NOT NULL
);
ALTER TABLE [dev].[Calendar]  ADD CONSTRAINT PK_Calendar_1 PRIMARY KEY NONCLUSTERED ([Year]) NOT ENFORCED;
ALTER TABLE [dev].[Calendar]  ADD CONSTRAINT UK_Calendar_ UNIQUE NONCLUSTERED ([Year]) NOT ENFORCED;

INSERT INTO [dev].[Calendar] ([Year]) VALUES (2020);
INSERT INTO [dev].[Calendar] ([Year]) VALUES (2020);
INSERT INTO [dev].[Calendar] ([Year]) VALUES (2020);
INSERT INTO [dev].[Calendar] ([Year]) VALUES (2020);

SELECT * FROM [dev].[Calendar]

 

 

Result:
Marusyk_0-1713876338246.png

 

1 ACCEPTED SOLUTION

You would need to build in de-duplicating in your loading processes.  Think of Fabric SQL like Databricks, Snowflake etc, none of these services have enforcable unique/foreign key constraints.

 

Unique and Foreign Key contraints are used by the Fabric SQL engine for optimisation purposes.  E.G if you define a column as unique and you use that column in a filter or join, once the SQL engine finds that value it knows it won't occur again (as an example).

 

So you won't use these contraints in the way you would with SQL Server.

 

-------------------------------------------------

If my post has been useful please consider

providing Kudos or marking as the Solution

-------------------------------------------------

View solution in original post

3 REPLIES 3
AndyDDC
Solution Sage
Solution Sage

Hi @Marusyk constraints are not actually enforced in Fabric Warehouses, hence the NOT ENFORCED keyword is required. This is a limitation of scale out sql services like Fabric SQL.  These constraints are more for engine optimisation 

so how to deny duplication then?

do you mean unique constraints mean nothing to me as a database admin?

If they only for the engine how can we use it?

You would need to build in de-duplicating in your loading processes.  Think of Fabric SQL like Databricks, Snowflake etc, none of these services have enforcable unique/foreign key constraints.

 

Unique and Foreign Key contraints are used by the Fabric SQL engine for optimisation purposes.  E.G if you define a column as unique and you use that column in a filter or join, once the SQL engine finds that value it knows it won't occur again (as an example).

 

So you won't use these contraints in the way you would with SQL Server.

 

-------------------------------------------------

If my post has been useful please consider

providing Kudos or marking as the Solution

-------------------------------------------------

Helpful resources

Announcements
April Fabric Update Carousel

Fabric Monthly Update - April 2024

Check out the April 2024 Fabric update to learn about new features.

Top Kudoed Authors