Thursday, March 12, 2015

Difference Between CHAR vs VARCHAR


CHAR

1.Used to store character string value of fixed length.

2.The maximum no. of characters the data type can hold is 255 characters.

3.It's 50% faster than VARCHAR.

4.Uses static memory allocation.

5.It will waste a lot of disk space.

6.The system does not have to search for the end of string.

7. Use Char when the data entries in a column are expected to be the same size.

EX. 
Declare test Char(100);
test='Test'-
Then "test" occupies 100 bytes first four bytes with values and rest with blank data.



VARCHAR

1.Used to store variable length alphanumeric data.

2.The maximum this data type can hold is up to 4000 characters.

3.It's slower than CHAR.

4.Uses dynamic memory allocation.

5.It will not occupy any space.

6.In VARCHAR the system has to first find the end of string and then go for searching.

7. VARCHAR when the data entries in a column are expected to vary considerably in size.

EX. 
Declare test VARCHAR100);
test='Test'-
Then "test" occupies only 4+2=6 bytes. First four bytes for value and other two bytes for variable length information.


Conclusion:

1.When using the fixed length data's in column like phone number, use CHAR.
2.When using the variable length data's in column like address , use VARCHAR.



No comments:

Post a Comment

How to create user in MY SQL

Create  a new MySQL user Account mysql > CREATE USER ' newuser '@'localhost' IDENTIFIED BY ' password '...