views:

557

answers:

8

Hi

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

Thanks, Mala

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

+8  A: 

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.

Eric
255+64 = 319, 320 is counting the @
Havenard
I'm using phpMyAdmin to set up the databases, and I don't see nvarchar anywhere... do I need to set that up manually with an SQL statement, or am I just missing it somewhere?
Mala
thisemailaddressisonly160charslong-thatishalfofwhatyousuggest-iknowperformancewontusuallybeanissue@butuserswithnoemailaddressunder100charswillNEVERbeanissue.com
MGOwen
+3  A: 

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.

Autocracy
+1  A: 

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.

Noon Silk
+1  A: 

I've never used the maximum. The following email address is only 94 characters:

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

and I can't imagine anyone actually using an email address like that. Too long to type and hard to remember.

If disk space in your db is an issue, and you don't mind if one user in a thousand has to use a secondary email address to use your site, go for 50 chars: [email protected]

(Then again, most of the time, disk space isn't an issue anymore, is it?)

MGOwen
A: 

Have a look here - very good explanation:

http://jacobsantos.com/2007/general/stop-doing-email-validation-the-wrong-way/

DmitryK
I'm not talking about validating. I have a SQL database, and I need to tell it how long to make the email field.
Mala
Exactly! Have you read the article though? Quote:“…local-part of an e-mail address has a maximum of 64 characters … and the domain name a maximum of 255 characters.”This gives us 64 characters for left part, then "@" symbol, then 255 characters for the right part. This is 320 characters.
DmitryK
Also keep in mind that according to RFC2822 (http://www.faqs.org/rfcs/rfc2822.html) e-mail field as such may look like e.g.: "my name" <[email protected]>
DmitryK
Ah, thank you for the clarification. My question was more geared towards what is practical, as opposed to what is possible. Thanks for the article though :)
Mala
Well, I guess nvarchar(320) will be practical...
DmitryK
+6  A: 

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.

ZZ Coder
Thank goodness for some common sense. Everyone saying "make it 320!" please, *please* type out a 320 character email address, take a long hard look at it, and ask yourself whether anyone on earth would ever use such an address in your app.
MGOwen
+1  A: 

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.
Wez Furlong
A: 

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

GIbboK