我们有一个包含以下列的 PriceCombo 表:id(非空)、a_id(空)、b_id(空)、c_id(空)、折扣(非空)。我们正在使用此表来确定分配各种商品组合的价格。
a_id = id of region
b_id = id of merchandize category
c_id = id of customer category
Run Code Online (Sandbox Code Playgroud)
所以基本上,我们总是知道 a、b 和 c,但我们有一个灵活的折扣方案,这样每笔销售都按照我们能找到的最具体的规则进行折扣。
我们的问题是我们无法防止数据库中的重复,例如,我们希望防止在 a_id、b_id 和 c_id 中具有相同值的两行
a_id=1, b_id=1, c_id=1 and
a_id=1, b_id=1, c_id=1
Run Code Online (Sandbox Code Playgroud)
不被允许和
a_id=1, b_id=1, c_id=null and
a_id=1, b_id=1, c_id=null
Run Code Online (Sandbox Code Playgroud)
不被允许,但
a_id=1, b_id=1, c_id=null and
a_id=1, b_id=null, c_id=null
Run Code Online (Sandbox Code Playgroud)
是允许的,因为这些在这 3 列方面不相同。
我在任何地方都没有找到关于如何创建唯一约束的答案,该约束既跨越多列又允许空值但不允许“相同”(至少在约束中指定的列方面)行