In the MySQL database version of varchar type below 4. 1, nvarchar (characters storing Unicode data type) is stored as 2 bytes, which is usually input in Chinese or other languages and is not easy to be garbled; Varchar: Chinese characters are 2 bytes, and other characters are stored as 1 byte. Varchar is suitable for inputting English and numbers.
In version 4.0, varchar(20) means 20 bytes. If you store UTF8 Chinese characters, you can only store 6 characters (3 bytes for each Chinese character); After version 5.0, varchar(20) means 20 characters. Whether storing numbers, letters or UTF8 Chinese characters (3 bytes for each Chinese character), you can store 20 characters with a maximum size of 65532 bytes; Varchar(20) is only 20 bytes at most in Mysql4, but Mysql5 has different storage sizes according to different codes. The specific rules are as follows:
A) storage restrictions
The Varchar field stores the actual content independently of the clustered index, and the actual length is indicated by 1 to 2 bytes at the beginning of the content (it takes 2 bytes for the length to exceed 255), so the maximum length cannot exceed 65535.
B) code length limitation
If the character type is gbk, each character takes up at most 2 bytes, and the maximum length cannot exceed 32766;
If the character type is utf8, each character takes up at most 3 bytes, and the maximum length cannot exceed 2 1845.
If the definition exceeds the above limit, the varchar field will be cast to the text type and a warning will be generated.
C) line length limitation
In practical application, it is the length of line definition that limits the length of varchar. MySQL requires that the defined length of a row cannot exceed 65535. If the defined table length exceeds this value, you will be prompted.
Error1118 (42000): The row size is too large. The maximum row size (excluding BLOBs) of the table type used is 65535. You must change some columns to text or spots.
-
Although the maximum length of vachar field in mysql is 65535, it can't store so much data. The maximum length can be 65533 (when non-empty fields are not allowed), and only 65532 when non-empty fields are allowed.