views:

854

answers:

6

The title pretty much frames the question. I have not used CHAR in years. Right now, I am reverse-engineering a database that has CHAR all over it, for primary keys, codes, etc. How about a CHAR(30) column?

Edit: So the general opinion seems to be that CHAR if perfectly fine for certain things. I, however, think that you can design a database schema that does not have a need for "these certain things", thus not requiring fixed-length strings. With the bit, uniqueidentifier, varchar, and text types, it seems that in a well-normalized schema you get a certain elegance that you don't get when you use encoded string values. Thinking in fixed lenghts, no offense meant, seems to be a relic of the mainframe days (I learned RPG II once myself). I believe it is obsolete, and I did not hear a convincing argument from you claiming otherwise.

+4  A: 

CHARs are still faster for processing than VARCHARs in the DBMS I know well. Their fixed size allow for optimizations that aren't possible with VARCHARs. In addition, the storage requirements are slightly less for CHARS since no length has to be stored, assuming most of the rows need to fully, or near-fully, populate the CHAR column.

This is less of an impact (in terms of percentage) with a CHAR(30) than a CHAR(4).

As to usage, I tend to use CHARs when either:

  • the fields will generally always be close to or at their maximum length (stock codes, employee IDs, etc); or
  • the lengths are short (less than 10).

Anywhere else, I use VARCHARs.

paxdiablo
> In addition, the storage requirements are slightly less for CHARS since no length has to be stored.That is only true if almost the whole allocated length is used (basically fixed size). Otherwise padding will be added (it least in Oracle), which may be more than storing the length.
Thilo
Good point, Thilo. Although that was (sort of) covered in my rules, I made it more explicit.
paxdiablo
@Pax - I agree with the "maximum length" thing but doesn't with "close to". And for similar reason I don't agree with the "less then 10 length" thingie. No negative vote intended.
Adeel Ansari
@Vinegar, if a VARCHAR uses 4 bytes for length and all rows waste less than 4 bytes, CHARs are better - that's where the "close to" comment originated. The 10-length is just my personal preference since CHARs are faster and 10 wasted bytes per row are irrelevant (on *my* platform).
paxdiablo
@Pax - I have to disagree with the <10 suggestion. If the length of the data element is fixed, I use a char. If the data might be 3 bytes or might be 8 or 9 bytes, why would I want to define it as char(9) and waste all that space (and deal with the padding a lot of DBMSs put in on chars)?
John M Gant
@jmgant000, it's okay to disagree :-) As I said, that's my preference - in my world (mainframes), I consider disk space is of less importance than raw speed (up to a point).
paxdiablo
+6  A: 

I use char(n) for codes, varchar(m) for descriptions. Char(n) seems to result in better performance because data doesn't need to move around when the size of contents change.

Otávio Décio
+2  A: 

Where the nature of the data dictates the length of the field, I use CHAR. Otherwise VARCHAR.

lukef
Right, and my point is: I don't see any more data of that nature (unless I introduce it myself).
cdonner
@cdonner There are still lots of common fields like phone numbers, zip codes, state abbreviations that aren't variable length. Also there could be internal codes and things like serial numbers, dept numbers, extensions, site IDs, store numbers, etc where the field isnt variable and a CHAR will work just fine and be the most optimal choice.
Pete
+3  A: 

I use CHAR when length of the value is fixed. For example we are generating a code or something based on some algorithm which returns the code with the specific fixed lenght lets say 13.

Otherwise, I found VARCHAR better. One more reason to use VARCHAR is that when you get the value back in your application you don't need to trim that value. In the case of CHAR you will get the full length of the column whether the value is filling it fully or not. It would get filled by spaces and you end up trimming every value, and forgetting that would lead to errors.

Adeel Ansari
A: 

Char isn't obsolete, it just should only be used if the length of the field should never vary. In the average database, this would be very few fields mostly some kind of code field like State Abbreviations which are a standard 2 character filed if you use the postal codes. Using Char where the filed length is varaible means that there will be a lot of trimming going on and that is extra, unnecessary work and the database should be refactored.

HLGEM
+1  A: 

For PostgreSQL, the documentation states that char() has no advantage in storage space over varchar(); the only difference is that it's blank-padded to the specified length.

Having said that, I still use char(1) or char(3) for one-character or three-character codes. I think that the clarity due to the type specifying what the column should contain provides value, even if there are no storage or performance advantages. And yes, I typically use check constraints or foreign key constraints as well. Apart from those cases, I generally just stick with text rather than using varchar(). Again, this is informed by the database implementation, which automatically switches from inline to out-of-line storage if the value is large enough, which some other database implementations don't do.

araqnid