Current location - Loan Platform Complete Network - Big data management - What is the maximum length of mysql VARCHAR?
What is the maximum length of mysql VARCHAR?

The maximum length of the varchar type of the MySQL database is limited to 255 in versions below 4.1, and its data range can be 0~255 or 1~255 (depending on different versions of the database). In versions above MySQL 5.0, the length of the varchar data type is supported to 65535, which means that 65532 bytes of data can be stored. The start bit and the end bit occupy 3 bytes, that is to say, in 4.1 Or in the following versions, data that needs to be stored in fixed TEXT or BLOB format can be stored in variable-length varchar, which can effectively reduce the size of the database file.

In versions below 4.1, the varchar type of the MySQL database is nvarchar (which stores characters of the Unicode data type), whether it is a character or a Chinese character, it is stored as 2 bytes and is generally used for Chinese Or input in other languages, so that it is not easy to garbled characters; varchar: Chinese characters are 2 bytes, and other characters are stored as 1 byte. Varchar is suitable for inputting English and numbers.

Below version 4.0, varchar(20) refers to 20 bytes. If UTF8 Chinese characters are stored, only 6 can be stored (each Chinese character is 3 bytes); above version 5.0, varchar(20 ), refers to 20 characters. Whether it is numbers, letters or UTF8 Chinese characters (each Chinese character is 3 bytes), 20 can be stored. The maximum size is 65532 bytes; varchar(20) is the largest in Mysql4. It is 20 bytes, but Mysql5 has different storage sizes depending on the encoding. The specific rules are as follows:

a) Storage restrictions

The varchar field stores the actual content separately in the cluster. Except for the cluster index, 1 to 2 bytes are used at the beginning of the content to indicate the actual length (2 bytes are required when the length exceeds 255), so the maximum length cannot exceed 65535.

b) Coding length limit

If the character type is gbk, each character can occupy up to 2 bytes, and the maximum length cannot exceed 32766;

Character type If it is utf8, each character can occupy up to 3 bytes, and the maximum length cannot exceed 21845.

If the definition exceeds the above limit, the varchar field will be forcibly converted to text type and a warning will be generated.

c) Line length limitation

What causes the varchar length limit in actual applications is the length of a line definition. MySQL requires that the defined length of a row cannot exceed 65535. If the defined table length exceeds this value, it will prompt

ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs.

------------------------------------------------ --------------------------

Although the maximum length of the vachar field type of mysql is 65535, it cannot be stored. With so much data, the maximum value can reach 65533 (when non-null fields are not allowed), and when non-null fields are allowed, it can only reach 65532.