如何在SQL Server 2008中创建“无重复”列?

时间:2022-08-28 08:34:14

I have a simple table in my SQL Server database. This table contains two columns: ID int, Name nvarchar(50). The ID column is the primary key for my table.

我的SQL Server数据库中有一个简单的表。此表包含两列:ID int,名称nvarchar(50)。ID列是表的主键。

I want the "Name" column to be "(No Duplicates)", like in Microsoft Access, But this column isn't the primary column. How could I do this?

我希望“Name”列是“(没有重复)”与Microsoft Access一样,本专栏也不是主专栏。我该怎么做呢?

3 个解决方案

#1


79  

Add a unique constraint for that column:

为该列添加惟一约束:

ALTER TABLE Foo ADD CONSTRAINT UQ_Name UNIQUE (Name)

To add it through SQL Management Studio UI:

通过SQL Management Studio UI添加:

  1. Open SQL Server Management Studio.
  2. 打开SQL Server Management Studio。
  3. Expand the Tables folder of the database where you wish to create the constraint.
  4. 展开您希望创建约束的数据库的Tables文件夹。
  5. Right-click the table where you wish to add the constraint and click Design.
  6. 右键单击希望添加约束的表,然后单击Design。
  7. In Table Designer, click on Indexes/Keys.
  8. 在表设计器中,单击索引/键。
  9. Click Add.
  10. 单击Add。
  11. Choose Unique Key in the Type drop-down list.
  12. 在类型下拉列表中选择唯一键。

To handle a situation where a unique constraint violation occurs, see for error 2601.

要处理发生唯一约束违反的情况,请参见错误2601。

#2


7  

This can also be done another way with the SSMS GUI if you prefer:

如果您愿意,也可以使用SSMS GUI进行另一种方式:

  1. Right click "Indexes" under your table in the SSMS Solution Explorer and click "New Index..." (I know you are looking to create a contstraint, not an index, but this is exactly what the ADD CONSTRAINT SQL script does.
  2. 在SSMS解决方案资源管理器中右键单击表下的“索引”,然后单击“New Index…”(我知道您希望创建一个contstraint,而不是一个索引,但是这正是ADD CONSTRAINT SQL脚本所做的事情。

如何在SQL Server 2008中创建“无重复”列?

  1. Give new index a name (e.g. "UQ_MyUniqueColumn"), check "Unique", and click "Add..."
  2. 给新索引起个名字。“uq_myecolumn”),检查“Unique”,点击“Add…”

如何在SQL Server 2008中创建“无重复”列?

  1. Check your column in the next window
  2. 查看下一个窗口中的列

如何在SQL Server 2008中创建“无重复”列?

  1. Click OK in both windows
  2. 在两个窗口中单击OK。

#3


5  

You are looking for the UNIQUE constraint.

您正在寻找唯一的约束。

#1


79  

Add a unique constraint for that column:

为该列添加惟一约束:

ALTER TABLE Foo ADD CONSTRAINT UQ_Name UNIQUE (Name)

To add it through SQL Management Studio UI:

通过SQL Management Studio UI添加:

  1. Open SQL Server Management Studio.
  2. 打开SQL Server Management Studio。
  3. Expand the Tables folder of the database where you wish to create the constraint.
  4. 展开您希望创建约束的数据库的Tables文件夹。
  5. Right-click the table where you wish to add the constraint and click Design.
  6. 右键单击希望添加约束的表,然后单击Design。
  7. In Table Designer, click on Indexes/Keys.
  8. 在表设计器中,单击索引/键。
  9. Click Add.
  10. 单击Add。
  11. Choose Unique Key in the Type drop-down list.
  12. 在类型下拉列表中选择唯一键。

To handle a situation where a unique constraint violation occurs, see for error 2601.

要处理发生唯一约束违反的情况,请参见错误2601。

#2


7  

This can also be done another way with the SSMS GUI if you prefer:

如果您愿意,也可以使用SSMS GUI进行另一种方式:

  1. Right click "Indexes" under your table in the SSMS Solution Explorer and click "New Index..." (I know you are looking to create a contstraint, not an index, but this is exactly what the ADD CONSTRAINT SQL script does.
  2. 在SSMS解决方案资源管理器中右键单击表下的“索引”,然后单击“New Index…”(我知道您希望创建一个contstraint,而不是一个索引,但是这正是ADD CONSTRAINT SQL脚本所做的事情。

如何在SQL Server 2008中创建“无重复”列?

  1. Give new index a name (e.g. "UQ_MyUniqueColumn"), check "Unique", and click "Add..."
  2. 给新索引起个名字。“uq_myecolumn”),检查“Unique”,点击“Add…”

如何在SQL Server 2008中创建“无重复”列?

  1. Check your column in the next window
  2. 查看下一个窗口中的列

如何在SQL Server 2008中创建“无重复”列?

  1. Click OK in both windows
  2. 在两个窗口中单击OK。

#3


5  

You are looking for the UNIQUE constraint.

您正在寻找唯一的约束。