索引 MySql TEXT 列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2889827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Indexing a MySql TEXT column?
提问by Pablo Santa Cruz
I ran this using MySql and it appears to not like TEXT
. With SQL server I use nvarchar(max)
What should I use in MySql? In other tables some fields will be descriptions and may be long so at the moment I am thinking that fixed length is bad.
我使用 MySql 运行它,它似乎不喜欢TEXT
. 使用 SQL 服务器时,我nvarchar(max)
应该在 MySql 中使用什么?在其他表格中,一些字段将是描述并且可能很长,所以目前我认为固定长度是不好的。
create table if not exists
misc_info (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
key TEXT UNIQUE NOT NULL,
value TEXT NOT NULL
)ENGINE=INNODB;
回答by Pablo Santa Cruz
You can't have a UNIQUE index on a text column in MySQL.
在 MySQL 中的文本列上不能有 UNIQUE 索引。
If you want to index on a TEXT or a BLOB field, you must specify a fixed length to do that.
如果要对 TEXT 或 BLOB 字段进行索引,则必须指定固定长度才能执行此操作。
From MySQL documentation:
从 MySQL文档:
BLOB and TEXT columns also can be indexed, but a prefix length must be given.
BLOB 和 TEXT 列也可以被索引,但必须给出前缀长度。
Example:
例子:
CREATE UNIQUE INDEX index_name ON misc_info (key(10));
回答by khai_khai
Two things:
两件事情:
- Key is a reserved word.
- You have to specify a length for a
UNIQUE(key) TEXT value
.
- 键是保留字。
- 您必须为 a 指定长度
UNIQUE(key) TEXT value
。
回答by Pekka
I think it chokes on the key
field name rather than the TEXT type (which should be perfectly fine).
我认为它会阻塞key
字段名称而不是 TEXT 类型(这应该很好)。
(And as @Pablo already said, memo fields can't be unique.)
(正如@Pablo 已经说过的,备忘录字段不能是唯一的。)