如何使 table b 的列仅对 table a 中的不同外键条目唯一

How to make a column of table b unique only for different foreign key entries in table a

假设有一个名为 Emails

的 table
CREATE TABLE Emails (
    ID nvarchar(40) NOT NULL,
    Link_ID int NOT NULL,
        GROUP int NOT NULL,
    Email nvarchar(100) NOT NULL)

和一个名为 Info

的 table
CREATE TABLE Info (
    CUST_ID int NOT NULL,
        Link_ID int NOT NULL)

这是 table 的简化视图,我无法更改它们的列。

我的目标是:

允许:

Cust_ID Group   Email
1       0   test1@allowed.com
1       1   test1@allowed.com

禁止:

Cust_ID Group   Email
1       0   test2@forbidden.com
1       0   test2@forbidden.com

Cust_ID Group   Email
1       0   test3@forbidden.com
2       1   test3@forbidden.com

Cust_ID Group   Email
1       0   test4@forbidden.com
2       0   test4@forbidden.com

如何在 MSSQL 2014 中实现这种独特性?

编辑: 我的目标是这样的:

您不能直接对多个表施加 UNIQUE 约束,我建议您可以采用以下方法之一。

  1. 您可以使用触发器在插入数据之前验证数据 如果被禁止则抛出错误。
  2. 在插入自身时,您可以使用 IF EXISTS.
  3. 检查数据是否被允许
  4. 创建一个 Indexed View 并对其设置 UNIQUE 约束。

我的建议是,创建一个索引视图并放置所需的约束。要了解更多信息,您可以查看 Enforcing Complex Constraints with Indexed Views