The best solution of dealing with the problem which about "Specified key was too long; max key length is 767 bytes" on MySQL 5(utf8mb4_bin).

晚上8:26

The best solution of dealing with the problem which about "Specified key was too long; max key length is 767 bytes" on MySQL 5(utf8mb4_bin) :

A. 一般不得已的最適解法 :

https://stackoverflow.com/questions/1814532/1071-specified-key-was-too-long-max-key-length-is-767-bytes

    1. 減少該PRIMARY KEY的欄位長度(data_length)。

    或是:

    2. 移除PRIMARY KEY並且改用UNIQUE key

        並且指定UNIQUE key的長度(the length of the UNIQUE key)

         2-1.

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

B. 我試過的最適解法

    1. 將該PRIMARY KEY的欄位資料型態(data_type)改為TEXT

    2. 指定PRIMARY KEY的長度(the length of the pk)

        PRIMARY KEY (`column1`, `column2`(190))

C. A. 和 B. 都非最佳解,因為:
This can be a problem. For example: I have field name (255) and add unique to this field at name(191) as I'm using utf8mb4. If I have my user add their name with 'IJUE3ump5fiUuCi16jSofYS234MLschW4wsIktKiBrTPOTKBK6Vteh5pNuz1tKjy...aO500mlJs' And the other user add their name with this 'IJUE3ump5fiUuCi16jSofYS234MLschW4wsIktKiBrTPOTKBK6Vteh5pNuz1tKjy...aO500mlJa' The different is the last character. It should pass validation not stuck at duplicate entry. – vee Jul 30 '16 at 15:24

D. 為了解決 C 的最佳解(如果column2的值都在UTF-8的字符集集合範圍內):

最佳解 --

     DROP TABLE `mytable`;
   delimiter $$
   create table mytable(

    `column1` varchar(48) COLLATE utf8mb4_bin NOT NULL,
    `column2` VARCHAR(255) COLLATE utf8_bin NOT NULL,
    `column3` BOOLEAN,

    PRIMARY KEY(ases_eventid,ases_entityid)

   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin$$

E. A. 和 B. 和 C. 的 最佳解D + plus

    plus :

        1. 如果column2的值都在UTF-8的字符集集合範圍外,也就是必須是utf8mb4_bin的話
     
            (1). 將column2的值變短後,再開另一個欄位當PK

            或

            (2). column2的值的欄位不當PK,以其他的不會重覆的值,例如:TimeStamp,當PK

  • Share:

You Might Also Like

0 意見