将电子邮件 VARCHAR(320) 存储为 UNIQUE,#1071 - 指定的键太长;最大密钥长度为 767 [英] Storing email VARCHAR(320) as UNIQUE, #1071 - Specified key was too long; max key length is 767

查看:71
本文介绍了将电子邮件 VARCHAR(320) 存储为 UNIQUE,#1071 - 指定的键太长;最大密钥长度为 767的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查过这个错误的原因是什么.因此,我知道尝试将电子邮件 VARCHAR(320) 设置为唯一键(320 * 3 = 960 字节),从而超出了限制(767 字节).

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).

但是,我使用 MySQL 作为数据库,我需要在我的应用程序中使用电子邮件值作为唯一键.你能告诉我,我应该改变什么来克服这个问题?

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?

推荐答案

在前 254 个字符左右创建唯一索引:

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

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

电子邮件应少于 254 个字符.

Emails should be shorter than 254 characters.

根据此答案,无论如何,最长的电子邮件是 254 个字符,因此这应该没问题.

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

这篇关于将电子邮件 VARCHAR(320) 存储为 UNIQUE,#1071 - 指定的键太长;最大密钥长度为 767的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆