简体   繁体   中英

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 :

在此输入图像描述

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:

在此输入图像描述

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 :

在此输入图像描述

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 . Then you can add or delete the column which you want to set unique key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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