I have the following tables in SQL Server 2019, where each footprint can have many stencils and each stencil can have many footprints.
CREATE TABLE dbo.Footprint
(
FootprintId INT IDENTITY(1,1) NOT NULL
CONSTRAINT PK_Footprint PRIMARY KEY CLUSTERED,
FootprintName NVARCHAR(255) NOT NULL
CONSTRAINT U_FootprintName UNIQUE
);
CREATE TABLE dbo.Stencil
(
StencilId INT IDENTITY(1,1) NOT NULL
CONSTRAINT PK_Stencil PRIMARY KEY CLUSTERED,
StencilName NVARCHAR(255) NOT NULL
CONSTRAINT U_Stencil UNIQUE (StencilName),
UseLocation NVARCHAR(255) NOT NULL,
PartNumber NVARCHAR(255) NULL
);
CREATE TABLE dbo.FootprintStencil
(
FootprintId INT NOT NULL,
StencilId INT NOT NULL,
CONSTRAINT PK_FootprintStencil
PRIMARY KEY CLUSTERED (FootprintId, StencilId),
CONSTRAINT FK_FootprintStencil_Footprint
FOREIGN KEY (FootprintId) REFERENCES dbo.Footprint (FootprintId),
CONSTRAINT FK_FootprintStencil_Stencil
FOREIGN KEY (StencilId) REFERENCES dbo.Stencil (StencilId)
);
Is there any way enforce in the database the constraint that
(Footprint.FootprintId, Stencil.UseLocation, Stencil.PartNumber)
must be unique as a set in the FootprintStencil
relationship?
I could probably do it with a trigger, but is there a better way?