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.
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
The difference between char and varchar
charField
|
varcharField
|
|
---|---|---|
255 characters | 65535 bytes, so the maximum number of characters in brackets must be calculated through encoding | |
Fixed length, the insufficient part is filled with hidden spaces | Variable length | |
There will be waste | More savings | |
High | Low | |
Omitted when inserting | Will not be omitted when inserted , omit | |
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 |
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)
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!