Why can't I put a constraint on nvarchar(max)?

后端 未结 3 1122
后悔当初
后悔当初 2020-12-09 14:24

Why can\'t I create a constraint on an nvarchar(max) column? SQL Server will not allow me to put a unique constraint on it. But, it allows me to create a unique

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 15:06

    nvarchar(max) is really a different data type from nvarchar(integer-length). It's characteristics are more like the deprecated text data type.

    If nvarchar(max) value becomes too large, like text, it will be stored outside the row (a row is constrained to 8000 bytes maximum) and a pointer to it is stored in the row itself. You cannot efficiently index such a large field and the fact that data can be stored somewhere else further complicates searching and scanning the index.
    A unique constraint requires an index to be enforced and as a result, SQL Server designers decided to disallow creating a unique constraint on it.

提交回复
热议问题