• <small id='QYLAM'></small><noframes id='QYLAM'>

    <i id='QYLAM'><tr id='QYLAM'><dt id='QYLAM'><q id='QYLAM'><span id='QYLAM'><b id='QYLAM'><form id='QYLAM'><ins id='QYLAM'></ins><ul id='QYLAM'></ul><sub id='QYLAM'></sub></form><legend id='QYLAM'></legend><bdo id='QYLAM'><pre id='QYLAM'><center id='QYLAM'></center></pre></bdo></b><th id='QYLAM'></th></span></q></dt></tr></i><div id='QYLAM'><tfoot id='QYLAM'></tfoot><dl id='QYLAM'><fieldset id='QYLAM'></fieldset></dl></div>

      <tfoot id='QYLAM'></tfoot>

      1. <legend id='QYLAM'><style id='QYLAM'><dir id='QYLAM'><q id='QYLAM'></q></dir></style></legend>
        • <bdo id='QYLAM'></bdo><ul id='QYLAM'></ul>
      2. MySQL 错误 #1071 - 指定的键太长;最大密钥长度为

        时间:2023-08-20
        <legend id='vGxA4'><style id='vGxA4'><dir id='vGxA4'><q id='vGxA4'></q></dir></style></legend>

        <i id='vGxA4'><tr id='vGxA4'><dt id='vGxA4'><q id='vGxA4'><span id='vGxA4'><b id='vGxA4'><form id='vGxA4'><ins id='vGxA4'></ins><ul id='vGxA4'></ul><sub id='vGxA4'></sub></form><legend id='vGxA4'></legend><bdo id='vGxA4'><pre id='vGxA4'><center id='vGxA4'></center></pre></bdo></b><th id='vGxA4'></th></span></q></dt></tr></i><div id='vGxA4'><tfoot id='vGxA4'></tfoot><dl id='vGxA4'><fieldset id='vGxA4'></fieldset></dl></div>

          1. <tfoot id='vGxA4'></tfoot>

                  <bdo id='vGxA4'></bdo><ul id='vGxA4'></ul>

                  <small id='vGxA4'></small><noframes id='vGxA4'>

                    <tbody id='vGxA4'></tbody>
                • 本文介绍了MySQL 错误 #1071 - 指定的键太长;最大密钥长度为 767 字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当我执行以下命令时:

                  ALTER TABLE `mytable` ADD UNIQUE (
                  `column1` ,
                  `column2`
                  );
                  

                  我收到此错误消息:

                  #1071 - Specified key was too long; max key length is 767 bytes
                  

                  关于 column1 和 column2 的信息:

                  Information about column1 and column2:

                  column1 varchar(20) utf8_general_ci
                  column2  varchar(500) utf8_general_ci
                  

                  我认为 varchar(20) 只需要 21 个字节,而 varchar(500) 只需要 501 个字节.所以总字节数是 522,小于 767.那么为什么我会收到错误消息?

                  I think varchar(20) only requires 21 bytes while varchar(500) only requires 501 bytes. So the total bytes are 522, less than 767. So why did I get the error message?

                  #1071 - Specified key was too long; max key length is 767 bytes
                  

                  推荐答案

                  767 bytes in MySQL version 5.6(和之前的版本),是 规定了 InnoDB 表的前缀限制.MyISAM 表的长度为 1,000 字节.在 MySQL 5.7 版(及更高版本)中,此限制已增加到 3072 字节.

                  767 bytes in MySQL version 5.6 (and prior versions), is the stated prefix limitation for InnoDB tables. It's 1,000 bytes long for MyISAM tables. This limit has been increased to 3072 bytes In MySQL version 5.7 (and upwards).

                  您还必须注意,如果您在 utf8mb4 编码的大字符或 varchar 字段上设置索引,则必须划分最大索引前缀长度767 字节(或 3072 字节)乘以 4 得到 191.这是因为 utf8mb4 字符的最大长度是四个字节.对于 utf8 字符,它将是三个字节,导致最大索引前缀长度为 255(或减去空终止符,254 个字符).

                  You also have to be aware that if you set an index on a big char or varchar field which is utf8mb4 encoded, you have to divide the max index prefix length of 767 bytes (or 3072 bytes) by 4 resulting in 191. This is because the maximum length of a utf8mb4 character is four bytes. For a utf8 character it would be three bytes resulting in max index prefix length of 255 (or minus null-terminator, 254 characters).

                  您可以选择的一种方法是为 VARCHAR 字段设置下限.

                  One option you have is to just place lower limit on your VARCHAR fields.

                  另一种选择(根据对此问题的回应)是获取列的子集而不是整个金额,即:

                  Another option (according to the response to this issue) is to get the subset of the column rather than the entire amount, i.e.:

                  ALTER TABLE `mytable` ADD UNIQUE ( column1(15), column2(200) );
                  

                  调整,因为您需要获取要应用的密钥,但我想知道是否值得检查有关此实体的数据模型以查看是否有可能进行改进,这将使您能够实施预期的业务规则而不会遇到问题MySQL 限制.

                  Tweak as you need to get the key to apply, but I wonder if it would be worth it to review your data model regarding this entity to see if there's improvements possible, which would allow you to implement the intended business rules without hitting the MySQL limitation.

                  这篇关于MySQL 错误 #1071 - 指定的键太长;最大密钥长度为 767 字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <i id='5E8vZ'><tr id='5E8vZ'><dt id='5E8vZ'><q id='5E8vZ'><span id='5E8vZ'><b id='5E8vZ'><form id='5E8vZ'><ins id='5E8vZ'></ins><ul id='5E8vZ'></ul><sub id='5E8vZ'></sub></form><legend id='5E8vZ'></legend><bdo id='5E8vZ'><pre id='5E8vZ'><center id='5E8vZ'></center></pre></bdo></b><th id='5E8vZ'></th></span></q></dt></tr></i><div id='5E8vZ'><tfoot id='5E8vZ'></tfoot><dl id='5E8vZ'><fieldset id='5E8vZ'></fieldset></dl></div>

                  1. <small id='5E8vZ'></small><noframes id='5E8vZ'>

                    • <bdo id='5E8vZ'></bdo><ul id='5E8vZ'></ul>
                    • <tfoot id='5E8vZ'></tfoot>
                        <legend id='5E8vZ'><style id='5E8vZ'><dir id='5E8vZ'><q id='5E8vZ'></q></dir></style></legend>

                            <tbody id='5E8vZ'></tbody>