我有一个主键为varchar(255)的表。在某些情况下,255个字符是不够的。我尝试将字段更改为文本,但我得到以下错误:
BLOB/TEXT column 'message_id' used in key specification without a key length
我该如何解决这个问题?
编辑:我还应该指出,这个表有一个多列的复合主键。
我有一个主键为varchar(255)的表。在某些情况下,255个字符是不够的。我尝试将字段更改为文本,但我得到以下错误:
BLOB/TEXT column 'message_id' used in key specification without a key length
我该如何解决这个问题?
编辑:我还应该指出,这个表有一个多列的复合主键。
当前回答
如果你的数据类型是TEXT -你将不得不改变它为VARCHAR 方案1:查询
ALTER TABLE table_name MODIFY COLUMN col_name datatype;
ALTER TABLE my_table MODIFY COLUMN my_col VARCHAR(255);
解决方案2:GUI (MySQL工作台) 步骤1 -在文本框中写入
Step2 -编辑数据类型,应用
其他回答
为了索引,必须将列类型更改为varchar或整型。
Nobody mentioned it so far... with utf8mb4 which is 4-byte and can also store emoticons (we should never more use 3-byte utf8) and we can avoid errors like Incorrect string value: \xF0\x9F\x98\... we should not use typical VARCHAR(255) but rather VARCHAR(191) because in case utf8mb4 and VARCHAR(255) same part of data are stored off-page and you can not create index for column VARCHAR(255) but for VARCHAR(191) you can. It is because the maximum indexed column size is 767 bytes for ROW_FORMAT=COMPACT or ROW_FORMAT=REDUNDANT.
For newer row formats ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED (which requires newer file format innodb_file_format=Barracuda not older Antelope) maximum indexed column size is 3072. It is available since MySQL >= 5.6.3 when innodb_large_prefix=1 (disabled by default for MySQL <= 5.7.6 and enabled by default for MySQL >= 5.7.7). So in this case we can use VARCHAR(768) for utf8mb4 (or VARCHAR(1024) for old utf8) for indexed column. Option innodb_large_prefix is deprecated since 5.7.7 because its behavior is built-in MySQL 8 (in this version is option removed).
去mysql编辑表->更改列类型为varchar(45)。
我曾经用过这个,也犯过你提到的错误:
CREATE INDEX idx_col1 ON my_table (col1);
然后我把它换成了这个,并解决了:
CREATE INDEX idx_col1 ON my_table (col1(255));
另外,如果你想在这个字段中使用索引,你应该使用MyISAM存储引擎和FULLTEXT索引类型。