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. 都非最佳解,因為:
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。
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
並且
2-1.
ALTER TABLE `mytable` ADD UNIQUE
B. 我試過的最適解法 :
1. 將該PRIMARY KEY的欄位資料型態(data_type)改為:TEXT。
2. 指定PRIMARY KEY的長度
PRIMARY KEY (`column1`, `column2`(190))
C. A. 和 B. 都非最佳解,因為:
This can be a problem. For example: I have(255) and add unique to this field at field name name 191) as I'm using utf8mb4. If I have my user add their name ( 'IJUE3ump5fiUuCi16jSofYS234MLschW4wsIktKiBrTPOTKBK6Vteh5pNuz1tKjy with aO500mlJs' And the other user ... their name with this 'IJUE3ump5fiUuCi16jSofYS234MLschW4wsIktKiBrTPOTKBK6Vteh5pNuz1tKjy add aO500mlJa' The ... is the last character. It should pass validation not stuck at duplicate entry. – different Jul 30 '16 at 15:24 vee
D. 為了解決 C 的最佳解(如果column2的值都在UTF-8的字符集集合範圍內):
最佳解 --
DROP TABLE
`column1` varchar
`column2` VARCHAR
`column3` BOOLEAN,
PRIMARY KEY
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin$$
E. A. 和 B. 和 C. 的 最佳解
1. 如果column2的值都在UTF-8的字符集集合範圍外,也就是必須是utf8mb4_bin的話,
(1). 將column2的值變短後,再開另一個欄位當PK,
或
(2). column2的值的欄位不當PK,以其他的不會重覆的值,例如:TimeStamp,當PK。


