views:

213

answers:

4

Hi

I have a database table, let's call it Countries. Countries all have names, short names and continents. Let's assume that the longest name of any country in the world is 54 characters long. Should I set the maximum size to 54 or should I set it to e.g. 64 or something else? Will my choice affect queries or storage in any way?

I know this might seem like pre-optimizing, but I find myself often choosing 32, 64, 128, etc. and I'd like to know if that matters.

Thanks

+1  A: 

Answers will be RDBMS specific:

If you are using SQL Server (2000 onwards), a varchar only uses space for the actual characters stored in it (plus a small overhead), up to the maximum size as declared.

As a general rule of thumb, don't optimise until you have a problem.

Mitch Wheat
+3  A: 

A solid DBMS should know what's best for it. If you need "54 chars" but the database can do better search-optimizations with 64 chars, it will expand the data-field internally automatically. If it doesn't, you will have saved 12 chars per row.

I don't think that's something you should worry about, just write down what you need.

Marcel J.
+1  A: 

If you're using SQL Server, I would strongly recommend NOT using VARCHAR(8000) or VARCHAR(MAX) for all fields..... you need to let common sense prevail here - make the field as big as you think you'll probably need it, add a little extra for unexpected length, and you're done :-)

Also, I think it's a good idea to keep it to a few lengths that you use over and over again. E.g. I use VARCHAR(255) for most fields like street address or e-mail etc. that could be a bit longer, VARCHAR(50) for things like phone numbers etc., and VARCHAR(20) for short strings like zip code and such.

Be pragmatic about it - find a style that works for you. Don't over-optimize, but don't go nuts the other way, either.

Marc

marc_s
So what's the rationale behind using 255 instead of 256?
Deniz Dogan
Don't know - possibly my Pascal heritage - strings were limited to 255 chars then :-) No particular reason - just a personal (bad?) habit
marc_s
I use 255 for many varchars. I thought that came from the email address RFC, but I could be wrong.
karim79
+1  A: 

Depends on the data type. For instance in ORACLE - a varchar2 data type would be a good choice for variable-length strings. You need only to define the maximum length of the string - varchar2(maxsize). This would mean that any string with a length of up to 10 would fit. The length of the column would depend on the actual string stored in it.

See. link text

In your case, if your absolutely sure that there is no country name longer than 54 characters, I would use variable-size data type of size 54.

al