0

我正在使用 SQL Server 2008。我有以下基本架构:

  • Table1( Field1, Field2)
  • Table2( Field3)

我需要Field1是唯一的(允许空值)。我可以这样做:

create unique nonclustered index IX_Table1_Field1 
on [Table1] (Field1)
where Field1 is not null

但是,我还需要在和Field1上是独一无二的。即如果我想使用的值正在被使用,或者它不能被允许。Field2Field3Field1Field1, Field2Field3

这可以通过索引实现吗?(我怎样才能做到这一点?)

4

1 回答 1

0

This sounds like a faulty design, to be honest. While what you're talking about could theoretically be achieved with triggers, the fact that you have such an unusual constraint suggests that the tables may not be modeled correctly.

Perhaps if you explained what the two tables and three fields represent, we could better understand how you arrived at this design and might be able to suggest alternatives.

Something to consider: although you only mentioned a limitation on Field1, usually a multi-field constraint involves all the fields. Is Field2 limited by fields 1, 2, and 3 as well? What about Field3? Are there any restrictions on how records can be changed or deleted?

于 2013-06-07T12:24:32.020 回答