Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between VARCHAR and CHAR?

Tags:

sql

mysql

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

I am trying to store MD5 hashes.

like image 908
steven Avatar asked Dec 11 '09 03:12

steven


People also ask

Which is better CHAR or VARCHAR?

CHAR is a fixed length field; VARCHAR is a variable length field. If you are storing strings with a wildly variable length such as names, then use a VARCHAR, if the length is always the same, then use a CHAR because it is slightly more size-efficient, and also slightly faster.

What is the difference between CHAR and VARCHAR give example?

To give you an example, CHAR(10) is a fixed-length non-Unicode string of length 10, while VARCHAR(10) is a variable-length non-Unicode string with a maximum length of 10. This means the actual length will depend upon the data.

What is VARCHAR and CHAR in SQL?

SQL varchar stores variable string length whereas SQL char stores fixed string length. This means SQL Server varchar holds only the characters we assign to it and char holds the maximum column space regardless of the string it holds.

What is the difference between CHAR 20 and VARCHAR 20 )?

The basic difference between Char and Varchar is that: char stores only fixed-length character string data types whereas varchar stores variable-length string where an upper limit of length is specified.


2 Answers

VARCHAR is variable-length.

CHAR is fixed length.

If your content is a fixed size, you'll get better performance with CHAR.

See the MySQL page on CHAR and VARCHAR Types for a detailed explanation (be sure to also read the comments).

like image 110
Anon. Avatar answered Oct 06 '22 23:10

Anon.


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.

VARCHAR

  1. Used to store variable length alphanumeric data.
  2. The maximum this data type can hold is up to
    • Pre-MySQL 5.0.3: 255 characters.
    • Post-MySQL 5.0.3: 65,535 characters shared for the row.
  3. It's slower than CHAR.
  4. Uses dynamic memory allocation.
like image 28
simplePerson43 Avatar answered Oct 06 '22 22:10

simplePerson43