231

I have an error at

Column 'key' in table 'misc_info' is of a type that is invalid for use as a key column in an index.

where key is a nvarchar(max). A quick google search finds that the maximum length of an index is 450 chars. However, this doesn't explain what a solution is. How do I create something like Dictionary where the key and value are both strings and obviously the key must be unique and is single? My sql statement was

create table [misc_info] (
[id] INTEGER PRIMARY KEY IDENTITY NOT NULL,
[key] nvarchar(max) UNIQUE NOT NULL,
[value] nvarchar(max) NOT NULL);
4
  • 20
    Do you really need your key to be (potentially) 4GB large AND unique? SqlServer does not allow this because checking uniqueness could potentially be a very time-consuming operation. May 19, 2010 at 8:50
  • 1
    @KlausByskovPedersen some more powerful DBMS like PostgreSQL is smart enough to allow it and index a digest instead. But you have a point.
    – Matthieu
    Oct 21, 2019 at 17:49
  • 1
    limiting varchar length does it Jul 29, 2020 at 15:00
  • If you are using entity framework with a code first approach, you will run into this error if you try to index variables of type string. Oct 15, 2021 at 14:48

5 Answers 5

292

A unique constraint can't be over 8000 bytes per row and will only use the first 900 bytes even then so the safest maximum size for your keys would be:

create table [misc_info]
( 
    [id] INTEGER PRIMARY KEY IDENTITY NOT NULL, 
    [key] nvarchar(450) UNIQUE NOT NULL, 
    [value] nvarchar(max) NOT NULL
)

i.e. the key can't be over 450 characters. If you can switch to varchar instead of nvarchar (e.g. if you don't need to store characters from more than one codepage) then that could increase to 900 characters.

8
  • 1
    For varchar, would the limit still be varchar(450) ?
    – Steam
    Dec 19, 2013 at 23:59
  • 12
    You have space to use either varchar(900) OR nvarchar(450). Dec 20, 2013 at 7:43
  • My understanding is that a varchar will take 4 bytes to determine the length of the item, meaning the actual limit needs to be varchar(896). Is this correct?
    – mrmillsy
    May 15, 2014 at 11:31
  • 2
    @mrmillsy The declared maximum size does not include the overhead (which is 2 bytes, not 4) and the overhead bytes are not included in the limit on maximum index row size. technet.microsoft.com/en-us/library/ms176089(v=sql.100).aspx May 15, 2014 at 11:50
  • 1
    @mrmillsy You're getting that message because you're including the ID1 int in the index. That int requires 4 bytes, in addition to the 900 bytes for the varchar. May 15, 2014 at 16:23
35

There is a limitation in SQL Server (up till 2008 R2) that varchar(MAX) and nvarchar(MAX) (and several other types like text, ntext ) cannot be used in indices. You have 2 options:
1. Set a limited size on the key field ex. nvarchar(100)
2. Create a check constraint that compares the value with all the keys in the table. The condition is:

([dbo].[CheckKey]([key])=(1))

and [dbo].[CheckKey] is a scalar function defined as:

CREATE FUNCTION [dbo].[CheckKey]
(
    @key nvarchar(max)
)
RETURNS bit
AS
BEGIN
    declare @res bit
    if exists(select * from key_value where [key] = @key)
        set @res = 0
    else
        set @res = 1

    return @res
END

But note that a native index is more performant than a check constraint so unless you really can't specify a length, don't use the check constraint.

1
  • Clever - nicer than triggers, I feel.
    – Neil Moss
    May 19, 2010 at 9:24
17

The only solution is to use less data in your Unique Index. Your key can be NVARCHAR(450) at most.

"SQL Server retains the 900-byte limit for the maximum total size of all index key columns."

Read more at MSDN

1
  • For varchar, would the limit still be varchar(450) ?
    – Steam
    Dec 20, 2013 at 0:00
12

A solution would be to declare your key as nvarchar(20).

2

Noting klaisbyskov's comment about your key length needing to be gigabytes in size, and assuming that you do in fact need this, then I think your only options are:

  1. use a hash of the key value
    • Create a column on nchar(40) (for a sha1 hash, for example),
    • put a unique key on the hash column.
    • generate the hash when saving or updating the record
  2. triggers to query the table for an existing match on insert or update.

Hashing comes with the caveat that one day, you might get a collision.

Triggers will scan the entire table.

Over to you...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.