Databases are intensely concerned with performance--speed and minimizing storage. In most other parts of the computer world, you are not going to be bothered about how many characters are in your character string; it could be one, it could be the entire contents of an encyclopedia; it's all just a string. In fact, a lot of languages don't even bother you about whether it's a string or a number.
But as computers get faster and gain more memory, people put more data into their databases and do fancier queries. For a database CPU and memory are just as limiting today as they were in the days of 64Kb main memory and 10Mb hard drives (on mainframe computers).
A fixed number of bytes is a lot easier to deal with than a variable length number. 10 bytes is a lot easier to deal with than 1,000,000. So your database wants you to give it a clue so it can give you a gigabyte of results from terrabytes of data in microseconds. If you're not using your database that hard, you won't need the speed it's offering and will be annoyed at the needless questions. But if you do need the performance, you'll be happy to give it some hints.
As noted in the other answers, use char
if it always uses a certain number of characters, varchar
if the length can vary but it doesn't get too large (my guess is most DB's treat it as a char
or text
depending on size), and text
if it could be any length. If your SQL tries to use a text
column, it might be best to summarize it somehow and put it in a char
or small varchar
column also, then do where
's and order by
's on that. Of course, that's only if performance matters to you.