共計(jì) 2596 個(gè)字符,預(yù)計(jì)需要花費(fèi) 7 分鐘才能閱讀完成。
本篇內(nèi)容介紹了“怎么解決數(shù)據(jù)庫(kù) ERROR 1071 (42000) 報(bào)錯(cuò)問(wèn)題”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓丸趣 TV 小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
今天在對(duì)一張表加索引時(shí)候出現(xiàn)如下報(bào)錯(cuò):
mysql ALTER TABLE ym_sys_dict ADD INDEX idx_dcode_dvalue (`dict_code`, `dict_value`);
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
查閱文檔時(shí)候,看到如下解釋?zhuān)?/p>
For CHAR, VARCHAR, BINARY, and VARBINARY columns, indexes can be created that use only the leading part of column values, using col_name(length) syntax to specify an index prefix length.
...
Prefixes can be up to 1000 bytes long (767 bytes for InnoDB tables). Note that prefix limits are measured in bytes, whereas the prefix length in CREATE TABLE statements is interpreted as number of characters ...
對(duì)于 myisam 和 innodb 存儲(chǔ)引擎,prefixes 的長(zhǎng)度限制分別為 1000 bytes 和 767 bytes。注意 prefix 的單位是 bytes,但是建表時(shí)我們指定的長(zhǎng)度單位是字符。
A utf8 character can use up to 3 bytes. Hence you cannot index columns or prefixes of columns longer than 333 (MyISAM) or 255 (InnoDB) utf8 characters. 以 utf8 字符集為例,一個(gè)字符占 3 個(gè) bytes。因此在 utf8 字符集下,對(duì) myisam 和 innodb 存儲(chǔ)引擎創(chuàng)建索引的單列長(zhǎng)度不能超過(guò) 333 個(gè)字符和 255 個(gè)字符
mysql 索引長(zhǎng)度限制:
1)單列索引:
mysql 在創(chuàng)建單列索引的時(shí)候?qū)α械拈L(zhǎng)度是有限制的 myisam 和 innodb 存儲(chǔ)引擎下長(zhǎng)度限制分別為 1000 bytes 和 767 bytes。(注意 bytes 和 character 的區(qū)別)
2) 組合索引:
對(duì)于 innodb 存儲(chǔ)引擎,多列索引的長(zhǎng)度限制如下:每個(gè)列的長(zhǎng)度不能大于 767 bytes;所有組成索引列的長(zhǎng)度和不能大于 3072 bytes
smallint 占 2 個(gè) bytes,timestamp 占 4 個(gè) bytes,utf8 字符集。utf8 字符集下,一個(gè) character 占三個(gè) byte。
對(duì)于這個(gè)問(wèn)題,解決方法有兩個(gè):
1)修改參數(shù) innodb_large_prefix,該參數(shù)默認(rèn)為 OFF,修改為 ON
mysql show variables like innodb_large_prefix
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| innodb_large_prefix | OFF |
+---------------------+-------+
2)修改字段長(zhǎng)度
查看表結(jié)構(gòu):
mysql show create table ym_sys_dict \G
*************************** 1. row ***************************
Table: ym_sys_dict
Create Table: CREATE TABLE `ym_sys_dict` ( `id` int(20) NOT NULL AUTO_INCREMENT,
`dict_name` varchar(100) NOT NULL COMMENT 字典名稱(chēng) ,
`dict_type` varchar(100) NOT NULL COMMENT 字典類(lèi)型 ,
`dict_code` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`dict_value` varchar(1000) DEFAULT NULL,
`order_num` int(11) DEFAULT 0 COMMENT 排序 ,
`remark` varchar(255) DEFAULT COMMENT 備注 ,
`del_flag` tinyint(4) DEFAULT 0 COMMENT 刪除標(biāo)記 -1:已刪除 0:正常 ,
PRIMARY KEY (`id`),
UNIQUE KEY `dict_type` (`dict_type`,`dict_code`)
) ENGINE=InnoDB AUTO_INCREMENT=93 DEFAULT CHARSET=utf8 COMMENT= 數(shù)據(jù)字典表
經(jīng)和開(kāi)發(fā)溝通,dict_value 字段長(zhǎng)度設(shè)置過(guò)長(zhǎng),改字段長(zhǎng)度為 100
alter table ym_sys_dict modify dict_value varchar(100);
然后可以正常添加索引
mysql ALTER TABLE ym_sys_dict ADD INDEX idx_dcode_dvalue (`dict_code`, `dict_value`);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
“怎么解決數(shù)據(jù)庫(kù) ERROR 1071 (42000) 報(bào)錯(cuò)問(wèn)題”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實(shí)用文章!