Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why we should use CHAR type when we have VARCHAR?

 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 35976
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!