I recently took a databases class here at my school, and this argument came up rather heatedly in the class. The teacher really likes the idea of a char while a few students see no point in it and that varchar are a lot better. This came up during the group’s presentation to the class on how they were going to design their database. It really got me thinking which one is better since they both had good arguments behind it. First, why varchar?
The students’ arguments were the fact that everyone in industry uses it now. Not only that everyone uses varchar, it is very convenient to use varchar. If you’re sure that something is within a certain amount of characters, why not just use a varchar and not wasting diskspace? Say, if you are creating a field for first names, why not just do a varchar(50)? That way, if a person’s name is only 3 characters, the varchar will contract to that size, then you wouldn’t have to waste the other 47 bytes. It seems that it would be pointless to use a char(50), since it will be padded until the 50th byte is filled with nothing. What about the argument for the other side from the teacher’s point of view?
His argument is totally on the side of the engineering process. We know that many people want software development to be an engineering field, but there are so many aspects to it that makes it not truely engineering. With this in mind, his point is that you need to know what you are putting inside of the database. In other engineering fields, when a building is being built, the engineers know exactly how much weight the building can safely handle. They know exactly how much concrete they need to keep something up, or they would know exactly what the outcome will be when they take certain steps during production. That is fairly untrue in the software arena.
If you just slap a varchar into the database into every textfield, then where is the engineering process? His argument is that this is the type of attitude that prevents a true establishment of the software engineering field. If you do not care about the information and data you are using, there are no good reasons to make this into a software engineering field.
I personally find that there’s merit to both arguments. Although, I find that the teacher’s argument is slightly stronger and seems more motivated than that of the students. However, the students do have a strong backing of the industry…or do they? I find myself using varchars for things that have a wider range of size variability while using char for something that has a lot less variability. What are your thoughts?

TheBusiness · November 8, 2009 at 3:12 am
A.K., for historical and teaching purposes, I feel it’s absolutely necessary for people to understand and use char; but in the real world, varchar is undoubtedly the better choice between the two. Like you said, you can reserve 50 bytes for a variable such as a first name, or you can set a varchar to use those bytes elsewhere. My point is this: if you are on a team writing lines of code that number up to the millions, you are going to want to save as many bytes as possible so that the program runs faster and more efficiently as well as uses as little space as possible. Those bytes add up, and despite the vast leaps in processor speeds and disk space, it seems like sloppy coding to just waste space unnecessarily. If in millions of lines of code, everyone practiced using char instead of vchar (or just generally used declarations that reserve way more space than necessary), the finished product is going to be sloppy. Sure, it might work properly, but it’s still using more bytes than necessary and thus not as efficient. It’s important to understand how much space is needed and why from the engineering standpoint, but only for teaching/historical purposes…not practical ones. As a final note, I’d like to say that I am by no means an expert programmer, so I really hope that I understand enough about this so that what I just said doesn’t make me look like a complete fool.
Admin comment by A.K. · November 8, 2009 at 3:27 pm
I would disagree with you that “varchar is undoubtedly the better choice between the two.” Memory is cheap nowadays, 1TB for ~ $100, roughly about 9.09e-9 (0.00000000909) cents per byte. Assuming one byte is wasted every row, that’s 0.009 cents for every 1 million rows, that is not much.
I would say that for bigger applications, it is even more pertinent to use char instead of varchar. A char is of fixed size, therefore it will be allocated directly in the table. To retrieve data from a column from a row, it is a matter of calculating the index and the offset. Something that can be done very quickly.
On the other hand, varchars are variable in size and thus cannot be directly allocated into a row. It is essentially a pointer to another location in memory (most likely the hard disk.) That adds up to one more dereference and one more read from the disk. So for millions of rows of data, the extra overhead incurred by a varchar can become a lot. I am sure that database engines have optimized a lot of this for the most part.
I think if you need a database to scale to millions of rows, a char will have much less overhead, and thus, will result in a faster database which can scale more easily.