问题
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
orROW_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