我有以下表格:
CREATE TABLE dbo.myTable]
(
ID1 [int] NOT NULL,
ID2 [int] NOT NULL,
StartDate smalldatetime NOT NULL,
EndDate smalldatetime NULL,
CONSTRAINT [PK_myTable1] PRIMARY KEY CLUSTERED (ID1 ASC, ID2 ASC, StartDate ASC)
) ON [PRIMARY]
我希望确保每个 ID1 和 ID2 的 StartDate
到 EndDate
期间是唯一的,没有重叠。
如何创建这样一个检查约束:
(
ID1 <> existingRow.ID1
or ID2 <> existingRow.ID2
)
or (
ID1 = existingRow.ID1
and ID2 = existingRow.ID2
and (
StartDate >= isnull(existingRow.EndDate, Startdate + 1)
or isnull(EndDate, existingRow.StartDate + 1) <= existingRow.StartDate
)
)
…或者一个带有如下条件的约束:
IF ID1 = existingRow.ID1 and ID2 = existingRow.ID2
CHECK (
StartDate >= isnull(existingRow.EndDate, Startdate + 1)
or isnull(EndDate, existingRow.StartDate + 1) <= existingRow.StartDate
)
提前感谢…
ID1
、ID2
组合,恰好有一行“有效”数据? - Damien_The_Unbeliever