• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why we should use CHAR type when we have VARCHAR?

 
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic