Storing email VARCHAR(320) as UNIQUE, #1071 - Specified key was too long; max key length is 767

别等时光非礼了梦想. 提交于 2019-12-12 04:15:53

问题


I have already checked what is the reason of this error. Therefore, I know I am exceeding the limit (767 byte) by trying to set email VARCHAR(320) as a UNIQUE key (320 * 3 = 960 byte).

However, I am using MySQL as a database and I need to use the email value as a unique key in my application. Could you please tell me, what should I change to overcome this problem?


回答1:


Create a unique index on the first 254 characters or so:

create unique index idx_t_email on t(email(254));

Emails should be shorter than 254 characters.

According to this answer, the longest possible email is 254 characters anyway, so this should be fine.




回答2:


Assuming you are using MySQL 5.5.14 or newer, you can solve this problem by:

  • Enabling the innodb_large_prefix server setting
  • Enabling innodb_file_format = BARRACUDA
  • Using ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED

This will allow you to include columns up to 3072 bytes long in InnoDB indexes, so your 320 character column could be included in a unique index.

Read my blog post for more details:

http://mechanics.flite.com/blog/2014/07/29/using-innodb-large-prefix-to-avoid-error-1071/



来源:https://stackoverflow.com/questions/36338153/storing-email-varchar320-as-unique-1071-specified-key-was-too-long-max-ke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!