181

How do I create a unique constraint on an existing table in SQL Server 2005?

I am looking for both the TSQL and how to do it in the Database Diagram.

David Basarab
  • 72,212
  • 42
  • 129
  • 156

10 Answers10

273

The SQL command is:

ALTER TABLE <tablename> ADD CONSTRAINT
            <constraintname> UNIQUE NONCLUSTERED
    (
                <columnname>
    )

See the full syntax here.

If you want to do it from a Database Diagram:

  • right-click on the table and select 'Indexes/Keys'
  • click the Add button to add a new index
  • enter the necessary info in the Properties on the right hand side:
    • the columns you want (click the ellipsis button to select)
    • set Is Unique to Yes
    • give it an appropriate name
Rory
  • 40,559
  • 52
  • 175
  • 261
  • 1
    It works... but... why the constraint is showed under INDEX folder instead CONSTRAINTS folder. It is shown with another icon, but anyway it should be at constraints folder. – Fernando Torres Feb 19 '16 at 13:36
84

In SQL Server Management Studio Express:

  • Right-click table, choose Modify or Design(For Later Versions)
  • Right-click field, choose Indexes/Keys...
  • Click Add
  • For Columns, select the field name you want to be unique.
  • For Type, choose Unique Key.
  • Click Close, Save the table.
SageMage
  • 1,096
  • 8
  • 16
James Lawruk
  • 30,112
  • 19
  • 130
  • 137
29
ALTER TABLE [TableName] ADD CONSTRAINT  [constraintName] UNIQUE ([columns])
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
WildJoe
  • 5,740
  • 3
  • 26
  • 30
15

Warning: Only one null row can be in the column you've set to be unique.

You can do this with a filtered index in SQL 2008:

CREATE UNIQUE NONCLUSTERED INDEX idx_col1
ON dbo.MyTable(col1)
WHERE col1 IS NOT NULL;

See Field value must be unique unless it is NULL for a range of answers.

Community
  • 1
  • 1
Squirrel
  • 1,355
  • 2
  • 16
  • 25
  • how do you do this in sql server 2005? – Maxrunner May 26 '15 at 19:14
  • 2
    It's not achievable in sql server 2005. I would strongly recommend upgrading to a more up-to-date RDBMS - it will be [officially unsupported](http://blogs.msdn.com/b/sqlreleaseservices/archive/2011/01/27/end-of-mainstream-support-for-sql-server-2005-and-end-of-service-pack-support-for-sql-server-2008-sp1.aspx) as of April 12 2016. – reedstonefood Jun 24 '15 at 13:26
13
ALTER TABLE dbo.<tablename> ADD CONSTRAINT
            <namingconventionconstraint> UNIQUE NONCLUSTERED
    (
                <columnname>
    ) ON [PRIMARY]
Ivan Bosnic
  • 1,916
  • 5
  • 21
  • 31
10

I also found you can do this via, the database diagrams.

By right clicking the table and selecting Indexes/Keys...

Click the 'Add' button, and change the columns to the column(s) you wish make unique.

Change Is Unique to Yes.

Click close and save the diagram, and it will add it to the table.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
8

You are looking for something like the following

ALTER TABLE dbo.doc_exz
ADD CONSTRAINT col_b_def
UNIQUE column_b

MSDN Docs

Thunder3
  • 1,120
  • 1
  • 7
  • 10
7

To create a UNIQUE constraint on one or multiple columns when the table is already created, use the following SQL:

ALTER TABLE TableName ADd UNIQUE (ColumnName1,ColumnName2, ColumnName3, ...)

To allow naming of a UNIQUE constraint for above query

ALTER TABLE TableName ADD CONSTRAINT un_constaint_name UNIQUE (ColumnName1,ColumnName2, ColumnName3, ...)

The query supported by MySQL / SQL Server / Oracle / MS Access.

Rafiq
  • 2,000
  • 2
  • 21
  • 27
  • `UNIQUE NONCLUSTERED` and ***options*** `PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON` ? – Kiquenet Nov 22 '16 at 08:51
5

In the management studio diagram choose the table, right click to add new column if desired, right-click on the column and choose "Check Constraints", there you can add one.

Gibbons
  • 355
  • 1
  • 3
  • 7
0

In some situations, it could be desirable to ensure the Unique key does not exists before create it. In such cases, the script below might help:

IF Exists(SELECT * FROM sys.indexes WHERE name Like '<index_name>')
    ALTER TABLE dbo.<target_table_name> DROP CONSTRAINT <index_name> 
GO

ALTER TABLE dbo.<target_table_name> ADD CONSTRAINT <index_name> UNIQUE NONCLUSTERED (<col_1>, <col_2>, ..., <col_n>) 
GO
Mario Vázquez
  • 717
  • 10
  • 9