Primary keys, foreign keys, and unique keys in Warehouse in Microsoft Fabric

Applies to: SQL analytics endpoint and Warehouse in Microsoft Fabric

Learn about table constraints in SQL analytics endpoint and Warehouse in Microsoft Fabric, including the primary key, foreign keys, and unique keys.

Important

To add or remove primary key, foreign key, or unique constraints, use ALTER TABLE.

Table constraints

SQL analytics endpoint and Warehouse in Microsoft Fabric support these table constraints:

  • PRIMARY KEY is only supported when NONCLUSTERED and NOT ENFORCED are both used.
  • UNIQUE constraint is only supported when NONCLUSTERED and NOT ENFORCED is used.
  • FOREIGN KEY is only supported when NOT ENFORCED is used.

For syntax, check ALTER TABLE.

Examples

Create a Warehouse in Microsoft Fabric table with a primary key:

CREATE TABLE PrimaryKeyTable (c1 INT NOT NULL, c2 INT);

ALTER TABLE PrimaryKeyTable ADD CONSTRAINT PK_PrimaryKeyTable PRIMARY KEY NONCLUSTERED (c1) NOT ENFORCED;

Create a Warehouse in Microsoft Fabric table with a unique constraint:

CREATE TABLE UniqueConstraintTable (c1 INT NOT NULL, c2 INT);

ALTER TABLE UniqueConstraintTable ADD CONSTRAINT UK_UniqueConstraintTablec1 UNIQUE NONCLUSTERED (c1) NOT ENFORCED;

Create a Warehouse in Microsoft Fabric table with a foreign key:

CREATE TABLE ForeignKeyReferenceTable (c1 INT NOT NULL);

ALTER TABLE ForeignKeyReferenceTable ADD CONSTRAINT PK_ForeignKeyReferenceTable PRIMARY KEY NONCLUSTERED (c1) NOT ENFORCED;

CREATE TABLE ForeignKeyTable (c1 INT NOT NULL, c2 INT);

ALTER TABLE ForeignKeyTable ADD CONSTRAINT FK_ForeignKeyTablec1 FOREIGN KEY (c1) REFERENCES ForeignKeyReferenceTable (c1) NOT ENFORCED;