在 SQL Server 2008 GUI 中添加唯一约束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13643023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:22:53  来源:igfitidea点击:

Add unique constraint in SQL Server 2008 GUI?

sqlsql-serverssmssql-server-2008

提问by mezamorphic

I have an existing table with data. I have just added a new column but I cannot find how to add a unique constraint on that column. Could someone please advise? Right-clicking and selecting "check constraints" wasn't helpful.

我有一个包含数据的现有表。我刚刚添加了一个新列,但找不到如何在该列上添加唯一约束。有人可以建议吗?右键单击并选择“检查约束”没有帮助。

回答by marc_s

You need to right-click in the table designer and pick Indexes/Keys:

您需要在表设计器中右键单击并选择Indexes/Keys

enter image description here

在此处输入图片说明

Then a dialog pops up and you can add a new index to the list of indices (on the left hand side) and define it to be a uniqueindex:

然后弹出一个对话框,您可以将新索引添加到索引列表(在左侧)并将其定义为unique索引:

enter image description here

在此处输入图片说明

回答by Guillermo Gutiérrez

Do right-click in the table designer and choose Indexes/Keys.

在表设计器中右键单击并选择Indexes/Keys

enter image description here

在此处输入图片说明

Indexes/Keys window will open. Click the button Add, to create the new index/key, and choose the column to be unique in the Columnsproperty:

索引/键窗口将打开。单击按钮Add,创建新的索引/键,并在Columns属性中选择要唯一的

enter image description here

在此处输入图片说明

In the properties of the new index/key, set Typeto Unique Key:

在新索引/键的属性中,将Type设置为Unique Key

enter image description here

在此处输入图片说明

And this is the generated code for the unique constraint:

这是唯一约束的生成代码:

ALTER TABLE [dbo].[Table_1] ADD  CONSTRAINT [IX_Table_1] UNIQUE NONCLUSTERED 
(
    [myUniqueColumn] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

回答by podiluska

Try right clicking and choosing Indexes/Keys, adding a new index and setting Is Uniqueto Yes.

尝试右键单击并选择Indexes/Keys,添加新索引并将其设置Is Unique为是。

回答by dinesh.k

You just Right Click from which column you need to add unique key from your table and you can select the Indexes/Keys. Then you can add or delete the column which you want to set unique key

您只需右键单击需要从表中添加唯一键的列,然后选择Indexes/Keys. 然后您可以添加或删除要设置唯一键的列