Add unique constraint in SQL Server 2008 GUI?

SqlSql ServerSsmsSql Server-2008

Sql Problem Overview


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.

Sql Solutions


Solution 1 - Sql

You need to right-click in the table designer and pick 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 unique index:

enter image description here

Solution 2 - Sql

Do right-click in the table designer and choose 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 Columns property:

enter image description here

In the properties of the new index/key, set Type to 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

Solution 3 - Sql

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

Solution 4 - Sql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmezamorphicView Question on Stackoverflow
Solution 1 - Sqlmarc_sView Answer on Stackoverflow
Solution 2 - SqlGuillermo GutiérrezView Answer on Stackoverflow
Solution 3 - SqlpodiluskaView Answer on Stackoverflow
Solution 4 - Sqldinesh.kView Answer on Stackoverflow