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

后端 未结 8 2081
轮回少年
轮回少年 2020-11-28 05:23

Here is an extracted portion of my query, reflecting the EMAIL_ADDRESS column data type and property:

EMAIL_ADDRESS CHARACTER VARYING(20) NOT NU         


        
相关标签:
8条回答
  • 2020-11-28 05:45

    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.

    0 讨论(0)
  • 2020-11-28 05:53

    Initially the maximum is 320 characters (64+1+255, as show in other answers) but as RFC 3696 Errata 1003 said:

    However, there is a restriction in RFC 2821 on the length of an address in MAIL and RCPT commands of 256 characters. Since addresses that do not fit in those fields are not normally useful, the upper limit on address lengths should normally be considered to be 256.

    And from RFC 5321 section 4.5.3.1.3:

    4.5.3.1.3. Path

    The maximum total length of a reverse-path or forward-path is 256 octets (including the punctuation and element separators)

    This is including the opening and closing brackets so it let us to only 254 octets of email address.

    But get in mind that the number of octets may not be equal to the numbers of characters (a char may have 2 or more octets). Also the RFC section 4.5.3.1 tell that there can be fields of more that the maximum and this is possible but not guarantied to servers to catch they correctly.

    And then you can/must use a VARCHAR(254) to store an email address.

    Note: In MySQL at least, a column declared as VARCHAR whit less or equal than 255 octets will be all stored as 1 byte + length (the 1 is to store the length) so no space is gained if used a lower limit.

    0 讨论(0)
提交回复
热议问题