SQL Server 2005 How Create a Unique Constraint?

SqlSql ServerConstraints

Sql Problem Overview


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.

Sql Solutions


Solution 1 - Sql

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

Solution 2 - Sql

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.

Solution 3 - Sql

ALTER TABLE [TableName] ADD CONSTRAINT  [constraintName] UNIQUE ([columns])

Solution 4 - Sql

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 https://stackoverflow.com/questions/377798/field-value-must-be-unique-unless-it-is-null for a range of answers.

Solution 5 - Sql

ALTER TABLE dbo.<tablename> ADD CONSTRAINT
            <namingconventionconstraint> UNIQUE NONCLUSTERED
    (
                <columnname>
    ) ON [PRIMARY]

Solution 6 - Sql

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.

Solution 7 - Sql

You are looking for something like the following

ALTER TABLE dbo.doc_exz
ADD CONSTRAINT col_b_def
UNIQUE column_b

MSDN Docs

Solution 8 - Sql

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.

Solution 9 - Sql

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.

Solution 10 - Sql

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

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
QuestionDavid BasarabView Question on Stackoverflow
Solution 1 - SqlRoryView Answer on Stackoverflow
Solution 2 - SqlJames LawrukView Answer on Stackoverflow
Solution 3 - SqlWildJoeView Answer on Stackoverflow
Solution 4 - SqlSquirrelView Answer on Stackoverflow
Solution 5 - SqlIvan BosnicView Answer on Stackoverflow
Solution 6 - SqlDavid BasarabView Answer on Stackoverflow
Solution 7 - SqlThunder3View Answer on Stackoverflow
Solution 8 - SqlRafiqView Answer on Stackoverflow
Solution 9 - SqlGibbonsView Answer on Stackoverflow
Solution 10 - SqlMario VázquezView Answer on Stackoverflow