【发布时间】:2021-06-03 05:38:50
【问题描述】:
我在 SQL Server 2019 中有以下表格,其中每个封装可以有多个模板,每个模板可以有多个封装。
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)
);
有没有办法在数据库中强制执行约束
(Footprint.FootprintId, Stencil.UseLocation, Stencil.PartNumber) 在FootprintStencil 关系中必须是唯一的?
我也许可以用触发器来做到这一点,但有更好的方法吗?
【问题讨论】:
标签: sql-server tsql