问题描述
我有一个错误
表 'misc_info' 中的列 'key' 的类型不能用作索引中的键列.
其中 key 是 nvarchar(max).谷歌快速搜索找到了这个.但是,它并没有解释什么是解决方案.我如何创建像 Dictionary 这样的东西,其中键和值都是字符串,显然键必须是唯一的并且是单一的?我的sql语句是
创建表 [misc_info] ([id] 整数主键标识非空,[key] nvarchar(max) UNIQUE NOT NULL,[值] nvarchar(max) NOT NULL);
唯一约束不能超过每行 8000 个字节,即使那样也只会使用前 900 个字节,因此您的键的最安全最大大小是:
创建表 [misc_info]([id] 整数主键标识非空,[key] nvarchar(450) 唯一非空,[值] nvarchar(max) 非空)
即密钥不能超过 450 个字符.如果您可以切换到 varchar
而不是 nvarchar
(例如,如果您不需要存储来自多个代码页的字符),那么这可能会增加到 900 个字符.>
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 found this. It, however, 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);
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.
这篇关于属于不能用作索引中的键列的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!