简体   繁体   English

MySQL MyISAM-无法向表添加新列

[英]MySQL MyISAM - not able to add a new column to table

I have the following table: 我有下表:

mysql> show create table keyword_links\G
*************************** 1. row ***************************
       Table: keyword_links
Create Table: CREATE TABLE `keyword_links` (
  `keyword_id` int(11) NOT NULL AUTO_INCREMENT,
  `keyword` tinytext NOT NULL,
  `link` tinytext,
  `weight` smallint(6) NOT NULL DEFAULT '0',
  `class_id` smallint(6) NOT NULL DEFAULT '0',
  `category_id` smallint(6) NOT NULL DEFAULT '0',
  `timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`keyword_id`),
  KEY `class_id` (`class_id`),
  KEY `category_id` (`category_id`),
  KEY `idx_keyword` (`keyword`(333))
) ENGINE=MyISAM AUTO_INCREMENT=5082 DEFAULT CHARSET=utf8

to which I am trying to add a new column, which is failing: 我尝试向其添加新列的操作失败了:

mysql> ALTER TABLE keyword_links ADD COLUMN list_id INT UNSIGNED NOT NULL DEFAULT 0;
ERROR 1170 (42000): BLOB/TEXT column 'keyword' used in key specification without a key length

The index on keyword column does have a key length of 333, so why is it failing and how to fix it? keyword列上的索引确实具有333的键长,那么为什么会失败以及如何解决呢?

UPDATE UPDATE

I tried reducing the size of the index on the keyword column from 333 to 255 and now I am able to add the new column successfully: 我尝试将keyword列的索引大小从333减少到255,现在我可以成功添加新列了:

ALTER TABLE keyword_links DROP INDEX idx_keyword;
CREATE INDEX index_keyword ON keyword_links (keyword(255));
ALTER TABLE keyword_links ADD COLUMN list_id INT UNSIGNED NOT NULL DEFAULT 0;

But I would still like to know what's going on. 但是我仍然想知道发生了什么。

索引键只允许我假设255个。

This is because of myisam's index's length limit of 1000 bytes ( see this link ) 这是因为myisam的索引的长度限制为1000个字节( 请参阅此链接

Please note that this is all about bytes, and that a character might take 2, 3 or even 4 bytes depending on your encoding, and also that an INT will always be 4 bytes . 请注意,这全部与字节有关,一个字符可能需要2、3甚至4个字节,具体取决于您的编码,并且INT 始终为4个字节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM