File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes why we should use CHAR type when we have VARCHAR? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "why we should use CHAR type when we have VARCHAR?" Watch "why we should use CHAR type when we have VARCHAR?" New topic
Author

why we should use CHAR type when we have VARCHAR?

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Hi
thank you for reading my post
why we should use Char when we have varchar?
based on what i read in database books, if we define a char variable with length of 25 and store 5 characters into it, it will occupy 25 characters space in database, mean while varchar does not occupy the amount of space we determined for it.
based on what i read, varchar occupied space is equal to number of characters that we really stored.

Thanks
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30913
    
158

Raminaa,
In theory, char can use less space if the amount of space needed is known. This isn't a good reason to use char though; just wanted to mention it.

In practice, it is useful to use char if you care exactly how long a field should be. For example, suppose I am storing a US social security number. It had better be exactly 9 characters. If not, it is meaningless. Here, char(9) has business meaning and the database helps enforce it. If someone accidentally adds an 8 character String, the database insert will fail - warning them of a data integrity problem.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why we should use CHAR type when we have VARCHAR?