views:

588

answers:

7

I am doing my first database project.

I have had the following query in the column of EMAIL_ADDRESS

         ...
         EMAIL_ADDRESS CHARACTER VARYING(20) NOT NULL, 
         ...

However, John Saunders uses VARYING(256). This suggests me that I have not necessarily understood the VARYING correctly. I understand it such that the length of an email-address is 20 characters in my case, while 256 for Jodn.

Context in John's code

CREATE TABLE so."User"
     (
         USER_ID SERIAL NOT NULL,
         USER_NAME CHARACTER VARYING(50) NOT NULL,
         EMAIL_ADDRESS CHARACTER VARYING(256) NOT NULL,      /// Here
         HASHED_PASSWORD so.HashedPassword NOT NULL,
         OPEN_ID CHARACTER VARYING(512),                                                         
         A_MODERATOR BOOLEAN,
         LOGGED_IN BOOLEAN,
         HAS_BEEN_SENT_A_MODERATOR_MESSAGE BOOLEAN,
         CONSTRAINT User_PK PRIMARY KEY(USER_ID)
     );

I have never seen email addresses longer than 20 characters, used by ordinary people.

What is the optimal length for an email address in a database?

+12  A: 

My work email address is more than 20 characters!

Read the appropriate RFC specification:

"The local-part of an e-mail address may be up to 64 characters long and the domain name may have a maximum of 255 characters"

Dan Diplo
Always code to formal specifications.
DanDan
+16  A: 

The maximum length of an email address is 320 characters.

Every email address is composed of two parts. The local part comes before the '@' sign, and the domain part follows it. In "[email protected]", the local part is "user", and the domain part is "example.com".

The local part must not exceed 64 characters and the domain part cannot be longer than 255 characters.

In sum, an email address can be 320 characters long at most.

I got that from here

Iain Hoult
It seems that it is the best to take 320 as the length.
Masi
+10  A: 
pageman
I don't understand why this is voted down. Yes, its better to allow all valid E-Mail Addresses and go with the suggested RFC compliant length, but this is very interesting. +1 from me.
Tim Büthe
@Tim Büthe thanks for the upvote - was wondering why the post got downvoted also :)
pageman
+1: for the figure
Masi
@Masi actually what's curious is that it's a Poisson distribution rather than a normal distribution - anyone have ideas why it's like that? :P
pageman
@pageman: The reason is that each event is randomly distributed AND each event is taken from the infinity space. - You get a similar distribution if you calculate the number of cars driving to RED such that you have time vs. number of cars driving to red in the axes.
Masi
Personally I like Benford's Law better: http://en.wikipedia.org/wiki/Benford%27s_law
Kitson
+1  A: 

As others have said, way bigger than 20. 256 + 64 sounds good to me, and is RFC compliant.

The only reason to not have such a large value for your database is if you are worrying about performance or space, and if you are doing that then I'm 99.99999999999999% sure that is premature optimization.

Go big.

Stu Thompson
VARCHAR only stored the number of characters needed (plus the length). Only issue I see is if you are fighting for space in the 8000 byte per row limit.
Richard Szalay
I am not fighting for space. I am fighting for the balance between security and usability.
Masi
+1  A: 

Variable character types in databases don't occupy unneeded space. Thus, there is no reason to constrain such fields as much as possible. Depending on the name of a person, the naming scheme used by their organization and their domain name, an address can easily exceed 20 characters.

There is no limit as to the length of local-part and domain-name in RFC-2822. RFC-2181 limits the domain name to 255 octets/characters though.

Again, since a varchar only uses the space actually used by the string you store, there is no reason to have a small limit for email address length. Just go with 512 and stop worrying. Everything else is premature optimization

VoidPointer
A: 

A CHAR(20) field will always take up 20 characters, whether you use it all or not. (Often padded with spaces at the end.) A VARCHAR(20) field will take up up to 20 characters, but may take up less. One benefit of CHAR()s constant width is fast jumping to a row in a table, because you can just calculate the index it must be on. The drawback is wasting space.

The benefit of constant-sized CHAR(x)'s is lost if you have any VARCHAR(x) columns in your table. I seem to recall that MySQL silently converted any CHAR() fields into VARCHAR() behind the scenes if some columns were VARCHAR()s.

A: 

Perhaps, have a look into the Email Address Length FAQ, which may have some additional information about this topic for you.