Thursday 20 March 2014

Diff between varchar and char in database

VARCHAR is variable-length , CHAR is fixed length.

CHAR Data Type is a Fixed Length type of Data Type.

For example, if you declare a variable/column of CHAR (12) data type, then it will always take 12 bytes irrespective of whether you are storing 1 character or 12 character in this variable or column. And in this example, as we have declared this variable/column as CHAR(12), so we can store max 12 characters in this column.

On the other hand, VARCHAR is a variable length Data Type. For example, if you declare a variable/column of VARCHAR (12) data type, it will take the number of bytes equal to the number of characters stored in this column. So, in this variable/column, if you are storing only one character, then it will take only one byte and if we are storing 12 characters, then it will take 12 bytes. And in this example, as we have declared this variable/column as VARCHAR (12), so we can store max 12 characters in this column. 

No comments:

Post a Comment