【问题标题】:Error adding unique constraint: Column in table is of a type that is invalid for use as a key column in an index [duplicate]添加唯一约束时出错:表中的列的类型对于用作索引中的键列无效[重复]
【发布时间】:2015-04-25 18:09:49
【问题描述】:

如果我已经在 Id 列上有一个主键,并且我有一个 Name 列,我想在该列上添加一个 Unique constraint,我该如何在 SQL Server 中执行此操作?

我尝试了以下方法:

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

错误:

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 表示为:

Name nvarchar(MAX) Allow Nulls

【问题讨论】:

    标签: sql-server


    【解决方案1】:

    这个的语法是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      相关资源
      最近更新 更多