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

What is the difference between char and varchar in mysql

青灯夜游
青灯夜游Original
2021-12-02 17:24:4129849browse

Difference: 1. The maximum length of the char field is 255 characters, and the maximum length of the varchar field is 65535 bytes; 2. The char type is wasteful in space usage, while the varchar type is relatively wasteful in space usage. Saving; 3. The search efficiency of char type is high, while the search efficiency of varchar type is relatively low.

What is the difference between char and varchar in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

The difference between char and varchar

##BehaviorMaximum length255 characters 65535 bytes, so the maximum number of characters in brackets must be calculated through encodingIs it a negative lengthFixed length, the insufficient part is filled with hidden spacesVariable lengthSpace usageThere will be wasteMore savings Search efficiencyHighLowTail spaceOmitted when insertingWill not be omitted when inserted , omit like when searching. The ' ' after like in the statement will not be omitted. The ' ' after like in the statement will not be omitted. , the spaces at the end of the field will not be omitted
charField varcharField

Summary

  • char(n) n in is the number of characters, ranging from 0 to 255 (an additional 1 to 2 bytes are needed to store the length)

  • varchar(n) n in is also the number of characters, but the maximum value needs to be calculated through encoding and cannot exceed 65535 bytes (1 to 2 bytes are needed to store the length)

  • General Use the char type for fixed-length data, such as ID number, mobile phone number, phone number, etc. If the length changes greatly, you can use the varchar type

  • Pay attention to the matching of trailing spaces, especially when inserting And when using like to search

[Related recommendations:

mysql video tutorial]

The above is the detailed content of What is the difference between char and varchar 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