How long should that field be?
I recently had the opportunity to refactor and normalize an extremely large database in MySQL. I love refactoring as a matter of course, and normalization just makes me all a-tingle, so it’s good work if I can get it.
I was noticing as I pored through the fields in the various tables, that many, many fields had the same varchar length. 90% of them were varchar(100), including zip codes. Well, that’s when the zip codes weren’t integers, but I digress.
The point is this: I did some checking online, and I couldn’t find a good guide to field sizes, so I thought I’d just share a little of an approach I feel is common-sensical and worthy of at least writing down.
Let’s take a look at a record, and go from there:
John Smith, 123 Somewhere Street Northwest, AmityMothertonvilePokeno, New Hampshire, 03101, 603-555-1212, jsmith2007@funky-nowhere.com
That’s pretty basically a bit of contact information. It’s normal, it’s average, and we all use it constantly in just about every app we develop using MySQL and PHP. So how long should we make the fields?
The name. “John Smith” is only ten characters, but what if someone has a longer name? What about “Luxaranimous Fitzgerald Luntensteinivilosky”? I think we could all agree that’s an egregiously long name, and it still comes in at 43 characters. So maybe we bump it up for a bit of a buffer, and call it 55 characters. Now I feel better, and I have my new “Fullname” field size for all time.
Next we have the street address. I once knew a person who lived on Martin Luther King Boulevard, and that’s decent size, so I’ll use that as a baseline. Assuming 6 characters for the number, and 5 for “north” or “south”, we come in at 39 characters. Add an apartment (Apartment 123) and we’re at 41 characters. Again, we pad it, and I come up with 55 as a decent field size for an address.
City and state are both easy. The longest city name in English is 60 characters long (look it up if you don’t believe me), and it is also the longest ‘place’ name in the English language. So that means that the city field is 60, and the state field, if you’re not just using abbreviations, is 60 as well. (Longest “place”, remember?)
Zip codes/postal codes in the US are easy, that’s nine without the dash, ten with the dash (12345-1234 or 123412345). Foreign postal codes are a bit longer in some cases, and according to This site, the longest postal code in the world is currently 10 characters long.
Next, let’s take a look at the dreaded phone number. I know this one always gave me fits when I started out, never could decide what to do with it. Do I store it into separate fields, do I exclude foreign phone numbers? If I don’t exclude foreign phone numbers, how do I make it familiar for all?
Relax, first of all. The longest phone number in existence, country code and all, is 14 characters by all accounts. The format varies wildly from country to country, so if you’re dealing with international phone numbers, you can still split them up the way you would with US numbers, just make sure you add a country code field. As long as the split fields end up adding to 14, you’re ok. If you want to add in the ability for separators, dashes, dots, morse code, whatever, jack it up to twenty. No need to go overboard. This methodology of course applies to mobile phone numbers and fax numbers, as well.
And then the email address field. My email address is “anthony.levensalor@opentravelsoftware.com“. That’s 41 characters right off the bat, and I know I don’t have the longest. In fact, the longest email address can be 64 + 1 + 255 characters long (user name, @ sign, domain name) according to the IETF.
Does that mean your email field needs to be 320 characters long? Well, technically, it is best practice to make sure you cover the extremes, so yeah. Just make sure you crunch that by allowing the field to be null. If that sounds silly to you, rest assured it sounds silly to me too. But somebody’s gonna have a 320 character long email address, and you know it.
So here’s the basic field specs, as much for my edification as yours:
full name: varchar(55)
address:Â varchar(55)
city:Â Â Â Â Â Â Â Â varchar(60)
state:Â Â Â Â Â Â varchar(60) [or varchar(2) if you're using the US abbreviations only]
zip:Â Â Â Â Â Â Â Â Â varchar(10)
phone:Â Â Â Â varchar(14) [that's just digits, no symbols. Of course, in the US, you only need 11]
email:Â Â Â Â Â Â varchar(320)
Until next time, I hope this helps you out.
~A!