The difference between char and varchar in mysql

1. The length of char is fixed and the length of varchar is not fixed

2.char(n) If the number of stored characters is less than n, then fill it with spaces, and then remove the spaces when querying. Therefore, the string stored in char type cannot have spaces at the end, and varchar is not limited to this.

3. char(n) fixed length, char(4) will occupy 4 bytes no matter how many characters are stored, varchar is the actual number of characters stored + 1 byte (n<=255) or 2 bytes (n>255), so varchar(4), storing 3 characters will occupy 4 bytes.
4.Char type string retrieval speed is faster than varchar type. Because the char length is fixed, it can store and query faster, but at the same time, there is a problem of wasting space. It can be said that space efficiency is wasted for time efficiency.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325955806&siteId=291194637