Explore Courses Blog Tutorials Interview Questions
0 votes
in SQL by (20.3k points)

What's the difference between VARCHAR and CHAR in MySQL?

I am trying to store MD5 hashes.

1 Answer

0 votes
by (40.7k points)

If your content is of fixed size, then you should use CHAR.

  •  CHAR: It is used for the fixed length of character data. The maximum length of character data should be 254 characters.

  • VARCHAR: It’s used for the variable length of character data. But it’s valid for a maximum of 4046 characters.

Differences between CHAR and VARCHAR in MySQL are as follows based on a few parameters:

1. Storage Size

CHAR value’s storage size is equal to the maximum size of this column that you declare while you are creating the table.

Varchar value’s storage size is the actual length of the data entered, not the maximum size for this column.

 2. Memory Allocation

Char uses static memory allocation

Varchar uses dynamic memory allocation

 3. Data Type

‘Char’ has the fixed-length data type which can be used to store character string value which is of fixed length.

Whereas, ‘Varchar’ is the variable-length data type that is used to store variable-length alphanumeric data.

 4. Length

Char variable’s length can be of any value from 0 to 255

While the length of varchar variable ranges from 0 to 65,535

 5. Data Entries

Char can be used when the data entries in a column are expected to be of the same size.

When the data entries in a column are expected to vary in size then Varchar can be used.

Related questions

0 votes
1 answer
asked Oct 19, 2020 in SQL by dev_sk2311 (45k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 24, 2019 in SQL by Tech4ever (20.3k points)

Browse Categories