如何修复“允许的最大密钥长度为900字节”。

时间:2014-02-25 09:56:38

标签: sql-server

我有以下表结构:

CREATE TABLE [dbo].[Formats]
    (
        [caption_id] [nvarchar](512) NOT NULL,
        Formats_exist BIT NOT NULL
    )
Result-: 1 row created

ALTER TABLE [dbo].[Formats] 
    ADD CONSTRAINT [pk__formats] 
     PRIMARY KEY CLUSTERED ([caption_id] ASC);

Result-: 1 row created 


    CREATE TABLE [dbo].[Signature]
    (
        [caption_id] [nvarchar](512) NOT NULL,
        [signature_id] [int] NOT NULL
    )

Result-: 1 row created


    ALTER TABLE [dbo].[Signature] 
    ADD CONSTRAINT [pk__signatures] 
     PRIMARY KEY CLUSTERED ([caption_id] ASC,[signature_id] ASC);

结果 - :

Msg 1944, Level 16, State 1, Line 1
Index 'pk__signatures' was not created. This index has a key length of at least 1028 bytes. The maximum permissible key length is 900 bytes.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

为什么我收到上述主键的错误,虽然它是nvarchar(512)并且超过900字节但是第一个没有错误。

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我也在两个陈述中都得到错误。

NVARCHAR需要2个字节的存储空间。

512 x 2 = 1024字节。

你需要512个字符吗?