How do you get the maximum number of characters 21842 that can be accommodated by the varchar type field in this table?
21842 = (65535-1-2-4)/3
MySQL requires that the defined length of a row cannot exceed 65535 (including multiple fields), so there are 65535.
The maximum effective length of a varchar depends on the maximum row size.
The reason for subtracting 1 is that the actual row storage starts from the 2nd byte.
The reason for subtracting 2 is that the 2 bytes in the varchar header represent the length.
The reason for subtracting 4 is that the int type of field id occupies 4 bytes.
The reason for dividing by 3 is that one utf8 character takes up 3 bytes.
If you change varchar(21842) to varchar(21844) in the test2 table, the following error will be reported:
1118 - Row size too large.
The maximum row size for the used table type, not counting BLOBs, is 65535.
This includes storage overhead, check the manual.
You have to change some columns to TEXT or BLOBs.
Selection of char, varchar and text types in MySQL:
If you know the fixed length, use char. For example, MD5 string is fixed to 32 bits.
Fields that change frequently use varchar.
For characters exceeding 255, you can only use varchar or text, not char.
Where varchar can be used, text is not used.
The character size of MySQL is related to the character set. If the character set is ascii, Chinese cannot be saved (it will be displayed as garbled characters). If it is UTF8, each character is 1-3 bytes.
varchar(6000) can store 6000 bytes. If the character set uses UTF-8, since UTF-8 is a "variable length encoding", English letters are compiled into one byte, and Chinese characters are usually 3 bytes, only extremely rare words will be encoded into 4-6 bytes. Therefore, it can be considered that when the encoding is UTF-8, 2000 Chinese characters can be stored in varchar(6000)
The length of varchar and char does not refer to the byte length, but the maximum number of characters in the current character set. For example, varchar 100, which stores ascii characters, can store up to 100 characters. When storing Chinese, it can store up to 100 characters. Not 33.
It can be seen that varchar(5) can store 5 characters, whether they are numbers, letters, or Chinese characters.
How do you get the maximum number of characters 21842 that can be accommodated by the varchar type field in this table?
21842 = (65535-1-2-4)/3
MySQL requires that the defined length of a row cannot exceed 65535 (including multiple fields), so there are 65535.
The maximum effective length of a varchar depends on the maximum row size.
The reason for subtracting 1 is that the actual row storage starts from the 2nd byte.
The reason for subtracting 2 is that the 2 bytes in the varchar header represent the length.
The reason for subtracting 4 is that the int type of field id occupies 4 bytes.
The reason for dividing by 3 is that one utf8 character takes up 3 bytes.
If you change varchar(21842) to varchar(21844) in the test2 table, the following error will be reported:
1118 - Row size too large.
The maximum row size for the used table type, not counting BLOBs, is 65535.
This includes storage overhead, check the manual.
You have to change some columns to TEXT or BLOBs.
Selection of char, varchar and text types in MySQL:
If you know the fixed length, use char. For example, MD5 string is fixed to 32 bits.
Fields that change frequently use varchar.
For characters exceeding 255, you can only use varchar or text, not char.
Where varchar can be used, text is not used.
The character size of MySQL is related to the character set. If the character set is ascii, Chinese cannot be saved (it will be displayed as garbled characters). If it is UTF8, each character is 1-3 bytes.
Speak with facts
Check the database character set first
Result: UTF8
Create table (Note:
varchar(10)
)Insert data
Cross ASCII characters, executed correctly
Ten Chinese characters, executed correctly
But the eleven ASCII or Chinese characters all reported errors, saying the data was too long.
Conclusion
Thelength is the character length of the current character set, not the byte length!
varchar(6000) can store 6000 bytes. If the character set uses UTF-8, since UTF-8 is a "variable length encoding", English letters are compiled into one byte, and Chinese characters are usually 3 bytes, only extremely rare words will be encoded into 4-6 bytes. Therefore, it can be considered that when the encoding is UTF-8, 2000 Chinese characters can be stored in varchar(6000)
The length of varchar and char does not refer to the byte length, but the maximum number of characters in the current character set. For example, varchar 100, which stores ascii characters, can store up to 100 characters. When storing Chinese, it can store up to 100 characters. Not 33.