SQL Server 2005 对两列的唯一约束 [英] SQL Server 2005 Unique constraint on two columns

查看:61
本文介绍了SQL Server 2005 对两列的唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 SQL Server 2005 中为两列添加唯一约束?所以可以说我有:

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

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

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

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?

好的,我们设法让它工作,感谢 OMG.转到表视图,选择两列,右键单击并选择索引/键"-常规选项卡,选择要唯一的列,然后将唯一"设置为true.这是使用表设计器.

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.

谢谢.

推荐答案

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

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

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

有关详细信息,请参阅此 MSDN 页面.

For more info, see this MSDN page.

这篇关于SQL Server 2005 对两列的唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆