Home » Mysql » What is the difference between CHAR and VARCHAR

What is the difference between CHAR and VARCHAR

What is the difference between CHAR and VARCHAR

CHAR and VARCHAR are both ASCII character data types, maximum number of character CHAR data type can hold is 255 character while VARCHAR can hold up to 4000 character.CHAR used static memory allocation while VARCHAR used dynamic memory allocation, following major differences are given below.

Char data Type

  1. Char is a Fixed length memory storage data type. store values are padded with space characters to match the specified length.
  2. CHAR takes up 1 byte per character
  3. Use Char data type when store data is expected to be the same size(Postal code)

Example: Declare a CHAR type in MySql
name CHAR(100);
Store a value "sameer" in this field

It means "sameer" occupies 100 bytes first six bytes with values and rest with blank data,and values are padded with space characters to match the specified length.

VarChar:

  1. Variable length memory storage(dynamically allocate memory storage)
  2. VARCHAR takes up 1 byte per character, + But 1 or 2 extra bytes are added to store the length of the data.
  3. Use VARCHAR when storage data in a column are expected to vary considerably in size.

Example: Declare a VARCHAR type in MySql
Declare name VarChar(100);

Now "sameer" occupies only 6+2=8 bytes. first six bytes for stored value and other two bytes are occupies for variable length information.

Result:

  1. When you are storing fixed length data in column like mobile number, then use Char data type.
  2. When you are storing variable length data in column like name,address and description.then use VARCHAR data type.

So If your want to store fixed size data, you'll get better performance with CHAR.

, , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">