MySQL char and varchar are very similar.
The maximum length of char is 255, and when it is not enough 255 characters, the right side complements the spaces padding.
The maximum varchar length is 65535.
VARCHAR (n) has one or two bytes more than char (n), which is used to determine the length of the varchar value. When value does not exceed 255 characters, an additional byte is required. When value exceeds 255 characters, 2 bytes are required.
Query efficiency comparison: Because varchar needs to position the length of value based on additional bytes, it is slower than char.
The row contains varchar and char cases |
Query efficiency |
Contains char and varchar |
Low |
Contains only varchar |
Low |
Contains only Char |
High |
CHAR (10) fills ' 1234567890 ', char (10) occupies 10 characters instead of 10 bytes. The specific occupancy byte is determined by the database collation set.
The difference between varchar (10) and varchar (255): When only one ' a ' is written, varchar (255) assigns 255 characters when the query requires a temporary table. VARCHAR (10) assigns only 10 characters.
MySQL Char varchar two or three thing