Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在外键上添加约束_Sql_Sql Server_Foreign Keys_Constraints_Erd - Fatal编程技术网

Sql 在外键上添加约束

Sql 在外键上添加约束,sql,sql-server,foreign-keys,constraints,erd,Sql,Sql Server,Foreign Keys,Constraints,Erd,我有一个超类升级,它有3个子类GiftCard,Offers,todaysdels Promotion(PromotionID,expiryDate) GiftCard(GiftCardID,points) where GiftCard.giftcardID references promotion TodaysDeals(TodaysDealsID,Discount) where TodaysDeals.TodaysDealsID references promotion Offers(Off

我有一个超类升级,它有3个子类GiftCard,Offers,todaysdels

Promotion(PromotionID,expiryDate)
GiftCard(GiftCardID,points)
where GiftCard.giftcardID references promotion
TodaysDeals(TodaysDealsID,Discount)
where TodaysDeals.TodaysDealsID references promotion
Offers(OffersID,Discount)
where offers.offersID references promotion
礼品卡促销与客户相关,而优惠与交易与产品相关。产品不能同时有报价和交易。 我想将promotionID添加到Products表中,确保与GiftCardID对应的任何promotionID都不在表中。这种方法是否可以在eerd中表示并转换为sql代码


我曾想过在交易和产品之间建立一种关系,在要约和产品之间建立另一种关系,但这并不能满足产品不能同时拥有要约和交易的约束,因为两个ID都将在产品表中表示为外键。

您可以将
PromoType
添加到表中,使
升级(PromotionID,promotionType)
唯一,并从其他表中引用此唯一键

Promotion(PromotionID, 
         PromoType check ('Deal','Offer', 'Gift'),
         expiryDate,
         UNIQUE(PromotionID,PromoType)
)

Product(
  PromoId,
  PromoType check ('Deal','Offer'),
  FK (PromoId,PromoType) ref Promotion(PromotionID,PromoType)
) 

不幸的是,不支持条件外键定义。@GordonLinoff哦,那你建议我怎么做?看起来你可以合并GiftCard,今天,在一个表中添加和提供PromotionType列,以指示这三种类型中的哪一种。@SeanLange但是这些实体中的每一个都与其他不同的实体有关系,所以我不想删除它们的表。您没有提到这一部分。但这并不意味着其他关系必须被消除。他们只需要指向新的表格。不幸的是,在这一点上,这实际上是抽象的,因为我们几乎没有手头表的详细信息,对项目和数据库的其余部分一无所知。有了更多的细节,也许有人能帮你找到解决办法。