Home  >  Article  >  Database  >  What is the difference between char and varchar data types in mysql

What is the difference between char and varchar data types in mysql

王林
王林Original
2020-06-24 15:54:518541browse

The difference between char and varchar data types in mysql is: the varchar type is used to store variable-length strings, and the char type is used to store fixed-length strings. Varchar is suitable for situations where the maximum length of a string column is larger than the average length, and char is suitable for situations where the length of a column is a fixed value.

What is the difference between char and varchar data types in mysql

(Recommended tutorial: mysql tutorial)

char and in mysql The difference between the varchar data type is:

The varchar type is used to store variable-length strings and is a relatively common string data type.

When the stored string is of variable length, varchar is more space-saving. Since varchar is variable-length, when using update, the line may become longer than the original, which requires additional work.

If the space occupied by a row increases and there is no extra space to store in the page, it is the innoDB storage engine that needs to split the page so that the row can be placed in the page.

The char type is used to store fixed-length strings.

When storing data, MySQL will delete all spaces at the end of the text. Therefore, even if you store: 'abc', note that there are spaces at the end of this string, it will also be stored when storing. Delete this space.

Let’s take a look at the usage scenarios:

Varchar applicable scenarios:

  • The maximum length of the string column is much larger than the average length;

  • When the string column is rarely updated, because there is no or little memory fragmentation problem;

  • Uses such complex data as UTF-8 Character set, each character uses a different number of bytes for storage;

char applicable scenarios:

  • The length of the column is fixed It is suitable when the value is appropriate, for example: MD5 ciphertext data.

The above is the detailed content of What is the difference between char and varchar data types in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn