SQL 电子邮件字段应该有多长?

发布于 2024-08-01 19:19:53 字数 190 浏览 4 评论 0原文

我认识到电子邮件地址基本上可以无限长,因此我在 varchar 电子邮件地址字段上施加的任何大小都是任意的。 但我想知道“标准”是什么? 你们能做多久? (名称字段的问题相同...)

更新: 显然,电子邮件地址的最大长度为 320(<=64 名称部分,<= 255 域)。 你用这个吗?

I recognize that an email address can basically be indefinitely long so any size I impose on my varchar email address field is going to be arbitrary. However, I was wondering what the "standard" is? How long do you guys make it? (same question for Name field...)

update: Apparently the max length for an email address is 320 (<=64 name part, <= 255 domain). Do you use this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夜灵血窟げ 2024-08-08 19:19:54

我使用 varchar(64) 我认为没有人可以拥有更长的电子邮件

I use varchar(64) i do not think anyone could have longer email

最好是你 2024-08-08 19:19:54

对于电子邮件,无论规格如何,我几乎总是使用 512 (nvarchar)。 名字和姓氏很相似。

实际上,您需要考虑一下您对拥有一些额外数据的关心程度。 对我来说,大多数情况下,这并不令人担心,所以我会犯保守的错误。 但如果您通过逻辑和准确的方式决定需要节省空间,那就这样做吧。 但总的来说,对场地大小保持保守,生活就会很好。

请注意,可能并非所有电子邮件客户端都支持 RFC,因此无论它说什么,您都可能会遇到不同的情况。

For email, regardless of the spec, I virtually always go with 512 (nvarchar). Names and surnames are similar.

Really, you need to look at how much you care about having a little extra data. For me, mostly, it's not a worry, so I'll err on the conservative side. But if you've decided, through logically and accurate means, that you'll need to conserve space, then do so. But in general, be conservative with field sizes, and life shall be good.

Note that probably not all email clients support the RFC, so regardless of what it says, you may encounter different things in the wild.

┼── 2024-08-08 19:19:54

以下电子邮件地址只有 94 个字符:

i.have.a.really.long.name.like.seetharam.krishnapillai@AReallyLongCompanyNameOfSomeKind.com.au

  • 组织是否会实际提供 你的电子邮件有那么长吗?
  • 如果他们足够愚蠢,您真的会使用这样的电子邮件地址吗?
  • 任何人都会吗?当然不会。 打字太长而且很难记住。

即使是 92 岁的技术恐惧症患者也会想出如何注册一个漂亮的简短 Gmail 地址,然后使用它,而不是将其输入到注册页面中。

磁盘空间可能不是问题,但允许用户输入字段比所需长度长很多倍至少存在两个问题:

  • 显示它们可能会弄乱您的 UI(最多它们会被切断,最坏的情况是它们会推动你的容器和边距)
  • 恶意用户可以用它们做你无法预料的事情(就像黑客使用免费的在线 API 来存储一堆数据)

我喜欢 50 个字符:

[电子邮件受保护]

如果某个用户在百万必须使用他们的其他电子邮件地址才能使用我的应用程序,就这样吧。

(统计数据显示,实际上没有人输入超过 40 个字符的电子邮件地址,请参见例如:ZZ Coder 的答案 https://stackoverflow。 com/a/1297352/87861

The following email address is only 94 characters:

i.have.a.really.long.name.like.seetharam.krishnapillai@AReallyLongCompanyNameOfSomeKind.com.au

  • Would an organisation actually give you an email that long?
  • If they were stupid enough to, would you actually use an email address like that?
  • Would anyone? Of course not. Too long to type and too hard to remember.

Even a 92-year-old technophobe would figure out how to sign up for a nice short gmail address, and just use that, rather than type this into your registration page.

Disk space probably isn't an issue, but there are at least two problems with allowing user input fields to be many times longer than they need to be:

  • Displaying them could mess up your UI (at best they will be cut off, at worst they push your containers and margins around)
  • Malicious users can do things with them you can't anticipate (like those cases where hackers used a free online API to store a bunch of data)

I like 50 chars:

[email protected]

If one user in a million has to use their other email address to use my app, so be it.

(Statistics show that no-one actually enters more than about 40 chars for email address, see e.g.: ZZ Coder's answer https://stackoverflow.com/a/1297352/87861)

半边脸i 2024-08-08 19:19:54

如果您真的对此犹豫不决,请创建用户名 varchar(60)、域 varchar(255)。 然后,您可以对域使用情况进行荒谬的统计,这比作为单个字段进行统计要快一些。 如果您对优化非常着迷,这也将使您的 SMTP 服务器能够以更少的连接/更好的批处理发送电子邮件。

If you're really being pendantic about it, make a username varchar(60), domain varchar(255). Then you can do ridiculous statistics on domain usage that is slightly faster than doing it as a single field. If you're feeling really gun-ho about optimization, that will also make your SMTP server able to send out emails with fewer connections / better batching.

听,心雨的声音 2024-08-08 19:19:54

RFC 5321(当前 SMTP 规范,废弃 RFC2821)指出:

4.5.3.1.1。 本地部分

用户最大总长度
名称或其他本地部分是 64
八位位组。

4.5.3.1.2。 域名

a的最大总长度
域名或号码为 255 个八位字节。

这仅涉及 localpart@domain,总共 320 个 ASCII(7 位)字符。

如果您计划规范化数据,也许可以通过将本地部分和域拆分为单独的字段,则需要记住其他事项:

  • 一种称为 VERP 的技术可能会生成自动生成邮件的完整长度本地部分(可能与您的使用无关) case)
  • 域不区分大小写; 建议小写域部分
  • localparts 区分大小写; [电子邮件受保护][email protected] 在技术上根据规范是不同的地址,尽管 domain.com 的政策可能 将两个地址视为等效。 最好将 localpart 大小写折叠限制到已知执行此操作的域。

RFC 5321 (the current SMTP spec, obsoletes RFC2821) states:

4.5.3.1.1. Local-part

The maximum total length of a user
name or other local-part is 64
octets.

4.5.3.1.2. Domain

The maximum total length of a
domain name or number is 255 octets.

This pertains to just localpart@domain, for a total of 320 ASCII (7-bit) characters.

If you plan to normalize your data, perhaps by splitting the localpart and domain into separate fields, additional things to keep in mind:

  • A technique known as VERP may result in full-length localparts for automatically generated mail (may not be relevant to your use case)
  • domains are case insensitive; recommend lowercasing the domain portion
  • localparts are case sensitive; [email protected] and [email protected] are technically different addresses per the specs, although the policy at the domain.com may be to treat the two addresses as equivalent. It's best to restrict localpart case folding to domains that are known to do this.
丘比特射中我 2024-08-08 19:19:54

我过去只做过 ​​255,因为这是根深蒂固的短输入标准,但又不能太短。 那,我是一个习惯的生物。

但是,由于最大值为 319,我会在该列上执行 nvarchar(320) 。 一定要记住@

nvarchar 不会使用您不需要的空间,因此如果您只有 20 个字符的电子邮件地址,它只会占用 20 个字节。 这与 nchar 形成对比,后者总是占据其最大值(它用空格在值的右侧填充)。

我还会使用 nvarchar 代替 varchar,因为它是 Unicode。 考虑到电子邮件地址的波动性,这绝对是正确的选择。

I've in the past just done 255 because that's the so-ingrained standard of short but not too short input. That, and I'm a creature of habit.

However, since the max is 319, I'd do nvarchar(320) on the column. Gotta remember the @!

nvarchar won't use the space that you don't need, so if you only have a 20 character email address, it will only take up 20 bytes. This is in contrast to a nchar which will always take up its maximum (it right-pads the value with spaces).

I'd also use nvarchar in lieu of varchar since it's Unicode. Given the volatility of email addresses, this is definitely the way to go.

≈。彩虹 2024-08-08 19:19:54

根据本文,基于正确的 RFC 文档,它不是 320,而是 254:
http://www.eph.co.uk/resources/email-地址长度常见问题解答/

编辑
使用 WayBack 机器:
https: //web.archive.org/web/20120222213813/http://www.eph.co.uk/resources/email-address-length-faq/

电子邮件地址的最大长度是多少?

254 个字符

对于最大有效电子邮件地址似乎有些混乱
地址大小。 大多数人认为它有 320 个字符(64
用户名字符 + 域 255 个字符 + 1
@ 符号的字符)。 其他来源建议 129 (64 + 1 + 64)
或 384(128+1+255,假设用户名长度加倍)
未来)。

这种混乱意味着您应该注意“稳健性原则”
(“开发人员应该仔细编写严格遵守
现有的 RFC,但接受并解析来自可能不存在的同行的输入
与那些 RFC 一致。” - 维基百科)在编写软件时
处理电子邮件地址。 此外,某些软件可能会瘫痪
通过天真​​的假设,例如认为 50 个字符就足够了
(例子)。 您的 200 个字符的电子邮件地址在技术上可能是有效的
但如果大多数网站或应用程序拒绝它,这对您没有帮助。

当前电子邮件的实际最大长度为 254 个字符:

“RFC 3696 的原始版本确实说 320 是最大值
长度,但 John Klensin (ICANN) 随后承认这是
错了。”

“这是由域最大长度的简单算术得出的
(255 个字符)+ 邮箱最大长度(64 个字符)+ @
符号 = 320 个字符。 错误的。 这个谣言实际上记录在
RFC3696 的原始版本。 它已在勘误表中得到纠正。
实际上 RFC5321 对路径元素有一个限制
256 个字符的 SMTP 事务。 但这包括尖括号
围绕电子邮件地址,因此电子邮件地址的最大长度为
254 个字符。”- Dominic Sayers

According to this text, based on the proper RFC documents, it's not 320 but 254:
http://www.eph.co.uk/resources/email-address-length-faq/

Edit:
Using WayBack Machine:
https://web.archive.org/web/20120222213813/http://www.eph.co.uk/resources/email-address-length-faq/

What is the maximum length of an email address?

254 characters

There appears to be some confusion over the maximum valid email
address size. Most people believe it to be 320 characters (64
characters for the username + 255 characters for the domain + 1
character for the @ symbol). Other sources suggest 129 (64 + 1 + 64)
or 384 (128+1+255, assuming the username doubles in length in the
future).

This confusion means you should heed the 'robustness principle'
("developers should carefully write software that adheres closely to
extant RFCs but accept and parse input from peers that might not be
consistent with those RFCs." - Wikipedia) when writing software that
deals with email addresses. Furthermore, some software may be crippled
by naive assumptions, e.g. thinking that 50 characters is adequate
(examples). Your 200 character email address may be technically valid
but that will not help you if most websites or applications reject it.

The actual maximum email length is currently 254 characters:

"The original version of RFC 3696 did indeed say 320 was the maximum
length, but John Klensin (ICANN) subsequently accepted this was
wrong."

"This arises from the simple arithmetic of maximum length of a domain
(255 characters) + maximum length of a mailbox (64 characters) + the @
symbol = 320 characters. Wrong. This canard is actually documented in
the original version of RFC3696. It was corrected in the errata.
There's actually a restriction from RFC5321 on the path element of an
SMTP transaction of 256 characters. But this includes angled brackets
around the email address, so the maximum length of an email address is
254 characters." - Dominic Sayers

可是我不能没有你 2024-08-08 19:19:53

理论上的限制确实很长,但您真的需要担心这些长电子邮件地址吗? 如果有人无法使用 100 个字符的电子邮件登录,您真的在乎吗? 我们实际上更希望他们不能。

一些统计数据可能会说明这个问题。 我们分析了一个包含超过 1000 万个电子邮件地址的数据库。 这些地址未经确认,因此存在无效地址。 这里有一些有趣的事实,

  1. 最长的有效长度是 89。
  2. 还有数百个更长的长度达到了我们专栏的限制(255),但通过目视检查它们显然是假的。
  3. 长度分布的峰值位于 19。
  4. 没有长尾。 38 以后一切都急剧下降。

我们通过丢弃任何超过 40 的内容来清理数据库。好消息是没有人抱怨,但坏消息是没有多少记录被清理。

The theoretical limit is really long but do you really need worry about these long Email addresses? If someone can't login with a 100-char Email, do you really care? We actually prefer they can't.

Some statistical data may shed some light on the issue. We analyzed a database with over 10 million Email addresses. These addresses are not confirmed so there are invalid ones. Here are some interesting facts,

  1. The longest valid one is 89.
  2. There are hundreds longer ones up to the limit of our column (255) but they are apparently fake by visual inspection.
  3. The peak of the length distribution is at 19.
  4. There isn't long tail. Everything falls off sharply after 38.

We cleaned up the DB by throwing away anything longer than 40. The good news is that no one has complained but the bad news is not many records got cleaned out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文