Primary Key & Unique Constraints in Azure Synapse Dedicated SQL Pool | NOT ENFORCED Explained
🔍 Introduction
In Azure Synapse Dedicated SQL Pools, constraints such as PRIMARY KEY and UNIQUE are allowed — but with a twist: they are NOT ENFORCED. This means they serve as metadata hints for the query optimizer rather than enforcing data integrity rules like in traditional SQL Server.
📌 Key Points
- FOREIGN KEY constraints are not supported.
- PRIMARY KEY and UNIQUE constraints must include
NOT ENFORCED
. - Duplicates are not prevented — you are responsible for ensuring data quality.
- These constraints can help the query optimizer generate better plans.
🛠️ Demo Overview
Step 1: Create Demo Table
CREATE TABLE dbo.Products (
ProductID INT NOT NULL,
ProductName NVARCHAR(100),
Category NVARCHAR(50)
)
WITH (DISTRIBUTION = ROUND_ROBIN);
Step 2: Insert Duplicate Values
INSERT INTO dbo.Products VALUES (1, 'Laptop', 'Electronics');
INSERT INTO dbo.Products VALUES (2, 'Tablet', 'Electronics');
INSERT INTO dbo.Products VALUES (1, 'Monitor', 'Electronics'); -- Duplicate ProductID
INSERT INTO dbo.Products VALUES (3, 'Desk', 'Furniture');
Step 3: Add UNIQUE Constraint (NOT ENFORCED)
ALTER TABLE dbo.Products
ADD CONSTRAINT UQ_ProductID UNIQUE (ProductID) NOT ENFORCED;
⚠️ The constraint will be created even though duplicates exist.
Step 4: Remove Duplicate Manually
DELETE FROM dbo.Products WHERE ProductName = 'Monitor';
Step 5: Replace UNIQUE with PRIMARY KEY (also NOT ENFORCED)
ALTER TABLE dbo.Products
DROP CONSTRAINT UQ_ProductID;
ALTER TABLE dbo.Products
ADD CONSTRAINT PK_ProductID PRIMARY KEY NONCLUSTERED (ProductID) NOT ENFORCED;
💡 Why Use These Constraints?
- Helps Synapse generate optimized query execution plans.
- Useful for BI/reporting layers that assume uniqueness.
- Important for documentation and data governance.
🚫 Limitations
- Constraints won't stop bad data — no runtime enforcement.
- You must manually ensure data quality via queries or ELT logic.
📺 Watch the Video Tutorial
📚 Credit: Content created with the help of ChatGPT and Gemini.