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添加:
- Open SQL Server Management Studio.
- 打开SQL Server Management Studio。
- Expand the Tables folder of the database where you wish to create the constraint.
- 展开您希望创建约束的数据库的Tables文件夹。
- Right-click the table where you wish to add the constraint and click Design.
- 右键单击希望添加约束的表,然后单击Design。
- In Table Designer, click on Indexes/Keys.
- 在表设计器中,单击索引/键。
- Click Add.
- 单击Add。
- Choose Unique Key in the Type drop-down list.
- 在类型下拉列表中选择唯一键。
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进行另一种方式:
- 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. - 在SSMS解决方案资源管理器中右键单击表下的“索引”,然后单击“New Index…”(我知道您希望创建一个contstraint,而不是一个索引,但是这正是ADD CONSTRAINT SQL脚本所做的事情。
- Give new index a name (e.g. "UQ_MyUniqueColumn"), check "Unique", and click "Add..."
- 给新索引起个名字。“uq_myecolumn”),检查“Unique”,点击“Add…”
- Check your column in the next window
- 查看下一个窗口中的列
- Click OK in both windows
- 在两个窗口中单击OK。
#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添加:
- Open SQL Server Management Studio.
- 打开SQL Server Management Studio。
- Expand the Tables folder of the database where you wish to create the constraint.
- 展开您希望创建约束的数据库的Tables文件夹。
- Right-click the table where you wish to add the constraint and click Design.
- 右键单击希望添加约束的表,然后单击Design。
- In Table Designer, click on Indexes/Keys.
- 在表设计器中,单击索引/键。
- Click Add.
- 单击Add。
- Choose Unique Key in the Type drop-down list.
- 在类型下拉列表中选择唯一键。
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进行另一种方式:
- 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. - 在SSMS解决方案资源管理器中右键单击表下的“索引”,然后单击“New Index…”(我知道您希望创建一个contstraint,而不是一个索引,但是这正是ADD CONSTRAINT SQL脚本所做的事情。
- Give new index a name (e.g. "UQ_MyUniqueColumn"), check "Unique", and click "Add..."
- 给新索引起个名字。“uq_myecolumn”),检查“Unique”,点击“Add…”
- Check your column in the next window
- 查看下一个窗口中的列
- Click OK in both windows
- 在两个窗口中单击OK。