SQL Server 2005对两列的唯一限制

时间:2022-07-22 04:23:25

How do you add a unique constraint in SQL Server 2005 to two columns? So lets say that I have:

如何将SQL Server 2005中的唯一约束添加到两列中?假设我有

PK, A, B ...
x1  1  1
x2  1  2
x3  2  1
x4  2  2

I should not be able to add another row 'x5' and have the values for A and B be 1,1 as they are already in the database in x1?

我不应该添加另一行" x5 "让A和B的值为1,1因为它们已经在x1的数据库中了?

Ok we managed to get it to work and thanks to OMG. Go to the table view, select the two columns, right click and select 'indexes/keys' - general tab, select the columns you want to be unique and then set 'is unique' to true. This is using the table designer.

好的,我们成功地让它工作了,感谢OMG。转到table视图,选择这两列,右击并选择“索引/键”- general选项卡,选择要惟一的列,然后将“is unique”设置为true。这是使用表设计器。

Thanks.

谢谢。

2 个解决方案

#1


35  

In SQL Server, a unique constraint is really implemented as a unique index. Use:

在SQL Server中,唯一约束实际上是作为唯一索引实现的。使用:

CREATE UNIQUE INDEX <uix_name> ON <table_name>(<col_A>, <col_B>)

For more info, see this MSDN page.

有关更多信息,请参见此MSDN页面。

#2


37  

ALTER TABLE YourTable
ADD CONSTRAINT UQ_YourTable_ConstraintName UNIQUE(A, B)

#1


35  

In SQL Server, a unique constraint is really implemented as a unique index. Use:

在SQL Server中,唯一约束实际上是作为唯一索引实现的。使用:

CREATE UNIQUE INDEX <uix_name> ON <table_name>(<col_A>, <col_B>)

For more info, see this MSDN page.

有关更多信息,请参见此MSDN页面。

#2


37  

ALTER TABLE YourTable
ADD CONSTRAINT UQ_YourTable_ConstraintName UNIQUE(A, B)