在两个列组合 SQL 上创建约束
Creating a constraints on two Column combination MSSQL
我有一个 table,它有两个列名 Parent 和 Child,数据如下所示。
| Parent | Child |
|---------------------|------------------|
| 100 | 101 |
|---------------------|------------------|
我也在这两列上添加了唯一约束,命令如下:
ALTER TABLE Example ADD CONSTRAINT UC_Example UNIQUE (parent,child);
现在,如果我尝试在 Parent 中插入 100,在 child 中插入 101,失败是正确的,但我想停止反向插入也
示例:101 父项和 100 子项也应该失败
有没有办法使用 sql 或过程或任何预定义的 sql 命令
这是一个带有函数约束的示例:
CREATE TABLE Tab1 (
Parent INT
,Child INT
)
INSERT INTO Tab1
VALUES (100,101),(100,102)
GO
---------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION dbo.fnc_RestrictedInsert (@NewParent INT, @NewChild INT)
RETURNS BIT
AS
BEGIN
DECLARE @Count INT = 0;
DECLARE @RetVal BIT = 1;
SELECT @Count = COUNT(*)
FROM Tab1
WHERE (Parent = @NewParent AND Child = @NewChild)
OR (Child = @NewParent AND Parent = @NewChild)
SET @RetVal = CASE
WHEN @Count > 1
THEN 0
ELSE 1
END
RETURN @RetVal;
END
---------------------------------------------------------------------------------------------------------------------------
ALTER TABLE Tab1 WITH NOCHECK ADD CONSTRAINT [CK_Ins] CHECK (([dbo].[fnc_RestrictedInsert]([Parent], [Child]) = (1)))
---------------------------------------------------------------------------------------------------------------------------
INSERT INTO Tab1 VALUES (100,101)
INSERT INTO Tab1 VALUES (101,100)
INSERT INTO Tab1 VALUES (100,103)
我会使用计算列来执行此操作。这样,就不需要定义额外的函数或触发器。
alter table example add min_id as (case when parentId < childId then parentId else childId end) persisted;
alter table example add max_id as (case when parentId < childId then childId else parentId end) persisted;
现在您可以创建独一无二的 index/constraint:
alter table example add constraint unq_parent_child
unique (min_id, max_id);
我有一个 table,它有两个列名 Parent 和 Child,数据如下所示。
| Parent | Child |
|---------------------|------------------|
| 100 | 101 |
|---------------------|------------------|
我也在这两列上添加了唯一约束,命令如下:
ALTER TABLE Example ADD CONSTRAINT UC_Example UNIQUE (parent,child);
现在,如果我尝试在 Parent 中插入 100,在 child 中插入 101,失败是正确的,但我想停止反向插入也 示例:101 父项和 100 子项也应该失败
有没有办法使用 sql 或过程或任何预定义的 sql 命令
这是一个带有函数约束的示例:
CREATE TABLE Tab1 (
Parent INT
,Child INT
)
INSERT INTO Tab1
VALUES (100,101),(100,102)
GO
---------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION dbo.fnc_RestrictedInsert (@NewParent INT, @NewChild INT)
RETURNS BIT
AS
BEGIN
DECLARE @Count INT = 0;
DECLARE @RetVal BIT = 1;
SELECT @Count = COUNT(*)
FROM Tab1
WHERE (Parent = @NewParent AND Child = @NewChild)
OR (Child = @NewParent AND Parent = @NewChild)
SET @RetVal = CASE
WHEN @Count > 1
THEN 0
ELSE 1
END
RETURN @RetVal;
END
---------------------------------------------------------------------------------------------------------------------------
ALTER TABLE Tab1 WITH NOCHECK ADD CONSTRAINT [CK_Ins] CHECK (([dbo].[fnc_RestrictedInsert]([Parent], [Child]) = (1)))
---------------------------------------------------------------------------------------------------------------------------
INSERT INTO Tab1 VALUES (100,101)
INSERT INTO Tab1 VALUES (101,100)
INSERT INTO Tab1 VALUES (100,103)
我会使用计算列来执行此操作。这样,就不需要定义额外的函数或触发器。
alter table example add min_id as (case when parentId < childId then parentId else childId end) persisted;
alter table example add max_id as (case when parentId < childId then childId else parentId end) persisted;
现在您可以创建独一无二的 index/constraint:
alter table example add constraint unq_parent_child
unique (min_id, max_id);