0

If I already have a primary key on the column Id and I have a column Name on which I want to add a Unique constraint, how can I do that in SQL Server?

I tried the following:

ALTER TABLE dbo.Hotels ADD CONSTRAINT
        UNQ_NAME UNIQUE NONCLUSTERED
(
            Name
)

Error:

Msg 1919, Level 16, State 1, Line 1
Column 'Name' in table 'Hotels' is of a type that is invalid for use as a key column in an index.

Name is represented as:

Name nvarchar(MAX) Allow Nulls
Pang
  • 9,564
  • 146
  • 81
  • 122
user1765862
  • 13,635
  • 28
  • 115
  • 220

1 Answers1

0

The syntax for this is:

ALTER TABLE <tablename> ADD CONSTRAINT
        <constraintname> UNIQUE NONCLUSTERED
(
            <columnname>
)
Alex
  • 21,273
  • 10
  • 61
  • 73