
July 4th, 2002, 04:49 AM
|
|
Junior Member
|
|
Join Date: Jul 2001
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Excuse my ignorance, but isn't the difference between char and varchar that when using the latter, only the amount of characters that equals the length of the string are saved, plus one byte indicating the length of the string. When using char, by contrast, a fixed amount of data is saved, no matter how long the contents may be.
So if you have, for example, a field called email that contains users' email addresses, averaging some 25 characters in length. You could keep this information in a char field with a fixed size of, say 40 characters, but then there would always be fairly much slack. Another thing is that when the user with an email address with one character more than you have defined as length arrives, problems arise.
A better solution would most probably be to use a varchar field with a size of at least 40 characters. My assumption is, therefore, that because only the amount of bytes needed to store the string, plus one byte for size info, are saved in a varchar field, there should be no reason whatsoever not to always use varchar fields of size 255, unless you are using the database itself to restrict the length of the (user) input to the varchar fields (which should be done with the overlying software anyway). Varchar(40) and varchar(255) both take the same amount of space, assuming (in the context of this example) that the strings stored are under 40 characters long.
Because the using of varchar fields with a size less than 255 characters is quite common thing to see in modern database table definitions, I'm just wondering what is the point behind this approach.
|