How long should SQL email fields be? [duplicate]

2019-01-21 05:25发布

This question already has an answer here:

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?

8条回答
干净又极端
2楼-- · 2019-01-21 06:05

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:

123456789.123456789.123456789@1234567890123456.com

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)

查看更多
放我归山
3楼-- · 2019-01-21 06:05

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

查看更多
登录 后发表回答