<div id=”article_content” class=”article_content csdn-tracking-statistics tracking-click” style=”overflow: hidden;” data-dsm=”post” data-mod=”popu_519″>
<div class=”htmledit_views”>
<h1 style=”color: #333333; font-family: Arial; font-size: 14px; line-height: 26px;”><span style=”font-size: 14pt;”>1.<span style=”font-size: 18pt;”>发现问题
<p style=”color: #333333; font-family: Arial; font-size: 14px; line-height: 26px;”> 今天在修改innodb表的某个列的长度时,报如下错误:
<p style=”color: #333333; font-family: Arial; font-size: 14px; line-height: 26px;”>
<div class=”dp-highlighter bg_html”>
<div class=”bar”>
<div class=”tools”>[html] <a class=”ViewSource” title=”view plain” onclick=”dp.sh.Toolbar.Command(‘ViewSource’,this);return false;” href=”#” target=”_self”>view plain<span class=”tracking-ad” data-mod=”popu_168″><span class=”tracking-ad” data-mod=”popu_168″> <a class=”CopyToClipboard” title=”copy” onclick=”dp.sh.Toolbar.Command(‘CopyToClipboard’,this);return false;” href=”#” target=”_self”>copy
<div style=”position: absolute; left: 540px; top: 879px; width: 16px; height: 16px; z-index: 99;”>
<span class=”tracking-ad” data-mod=”popu_169″> <a class=”PrintSource” title=”print” onclick=”dp.sh.Toolbar.Command(‘PrintSource’,this);return false;” href=”#” target=”_self”>print<a class=”About” title=”?” onclick=”dp.sh.Toolbar.Command(‘About’,this);return false;” href=”#” target=”_self”>?
- ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis767bytes
2.分析问题
2.1 问题原因分析
[html]
- ***************************1.row***************************
- Non_unique:1
- Seq_in_index:1
- Collation:A
- Sub_part:NULL
- Null:YES
- Comment:
- 1rowinset(0.00sec)
show index from test2\G; *************************** 1. row *************************** Table: test2 Non_unique: 1 Key_name: id Seq_in_index: 1 Column_name: id Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 1 row in set (0.00 sec)##删除id列的索引后, alter table test2 modify操作可以正常执行
2.2 关于mysql索引长度限制
[html]
- "ForCHAR,VARCHAR,BINARY,andVARBINARYcolumns,indexescanbecreatedthatuSEOnlytheleadingpartofcolumnvalues,usingcol_name(length)syntaxtospecifyanindexprefixlength.
- Prefixescanbeupto1000byteslong(767bytesforInnoDBtables).Notethatprefixlimitsaremeasuredinbytes,whereastheprefixlengthinCREATETABLEstatementsisinterpretedasnumberofcharacters..."对于myisam和innodb存储引擎,prefixes的长度限制分别为1000bytes和767bytes。注意prefix的单位是bytes,但是建表时我们指定的长度单位是字符。
- Autf8charactercanuseupto3bytes.Henceyoucannotindexcolumnsorprefixesofcolumnslongerthan333(MyISAM)or255(InnoDB)utf8characters.以utf8字符集为例,一个字符占3个bytes。因此在utf8字符集下,对myisam和innodb存储引擎创建索引的单列长度不能超过333个字符和255个字符
>从5.6的官方文档中我们能找到如下双引号中解释 "For CHAR,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 ...">>>对于myisam和innodb存储引擎,prefixes的长度限制分别为1000 bytes和767 bytes。注意prefix的单位是bytes,但是建表时我们指定的长度单位是字符。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字符集为例,一个字符占3个bytes。因此在utf8字符集下,对myisam和innodb存储引擎创建索引的单列长度不能超过333个字符和255个字符
<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools">[html] <a class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="#" target="_self">copy
<div style="position: absolute; left: 540px; top: 1860px; width: 16px; height: 16px; z-index: 99;">
<span class="tracking-ad" data-mod="popu_169"> <a class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="#" target="_self">?
- ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis767bytes
##对于innodb表,索引列长度大于255时,会报错。
<p style="font-family: Arial; font-size: 14px; line-height: 26px;"> 从上面可以看出,mysql 在创建单列索引的时候对列的长度是有限制的 myisam和innodb存储引擎下长度限制分别为1000 bytes和767 bytes。(注意bytes和character的区别)
2) 组合索引长度限制
<p style="font-family: Arial; font-size: 14px; line-height: 26px;"> 对于innodb存储引擎,多列索引的长度限制如下:<p style="font-family: Arial; font-size: 14px; line-height: 26px;">每个列的长度不能大于767 bytes;所有组成索引列的长度和不能大于3072 bytes
<p style="font-family: Arial; font-size: 14px; line-height: 26px;">参考如下例子(下面默认使用的使用innodb存储引擎,smallint 占2个bytes,timestamp占4个bytes,utf8字符集。utf8字符集下,一个character占三个byte)
<p style="font-family: Arial; font-size: 14px; line-height: 26px;">
<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools">[html] <a class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="#" target="_self">copy
<div style="position: absolute; left: 540px; top: 2279px; width: 16px; height: 16px; z-index: 99;">
<span class="tracking-ad" data-mod="popu_169"> <a class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="#" target="_self">?
- QueryOK,0rowsaffected(0.11sec)=
- mysqldroptabletest3;
- ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis767bytes
- ERROR1071(42000):Specifiedkeywastoolong;maxkeylengthis3072bytes
- QueryOK,0rowsaffected(0.09sec)
create table test3(id varchar(255),key(id));
Query OK,0 rows affected (0.11 sec) >>
mysql> drop table test3;
Query OK,0 rows affected (0.03 sec)