简体   繁体   English

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

[英]Add unique constraint in SQL Server 2008 GUI?

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. 右键单击并选择“检查约束”没有用。

You need to right-click in the table designer and pick Indexes/Keys : 您需要右键单击表设计器并选择Indexes/Keys

在此输入图像描述

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 unique index: 然后会弹出一个对话框,您可以在索引列表中添加新索引(在左侧)并将其定义为unique索引:

在此输入图像描述

Do right-click in the table designer and choose Indexes/Keys . 在表设计器中单击鼠标右键,然后选择“ 索引/键”

在此输入图像描述

Indexes/Keys window will open. 索引/键窗口将打开。 Click the button Add , to create the new index/key, and choose the column to be unique in the Columns property: 单击“ 添加 ”按钮以创建新索引/键,并在“ 列”属性中选择要唯一的

在此输入图像描述

In the properties of the new index/key, set Type to Unique Key : 在新索引/键的属性中,将Type设置为Unique Key

在此输入图像描述

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

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

You just Right Click from which column you need to add unique key from your table and you can select the Indexes/Keys . 您只需右键单击您需要从表中添加唯一键的列,然后您可以选择Indexes/Keys Then you can add or delete the column which you want to set unique key 然后,您可以添加或删除要设置唯一键的列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM