Question about the length of varchar Chinese and English in MySQL 5.1

Source: Internet
Author: User

Today, it is also a strange idea. MySQL verifies that varchar (15) will save 15 Chinese characters, and the first 15 will be automatically intercepted if the number exceeds 15. This concept may come from SQL server. In the varchar type, Chinese occupies two bytes. Therefore, only half of the varchar length can be saved in Chinese. However, in MySQL, both Chinese and English are length values, but the maximum length of varchar is different when different character encoding is selected for the database, however, the length of English and Chinese characters in the same encoding is the same.

In MySQL5, The varchar field type is described as follows: varchar (m) variable-length string. M indicates the maximum column length. M ranges from 0 to 65,535. (The maximum actual length of a VARCHAR is determined by the maximum row size and the character set used. The maximum valid length is 65,532 bytes ).

MySQL 5.1 complies with standard SQL specifications and does not delete trailing spaces of VARCHAR values. When VARCHAR is saved, it uses a prefix of one or two bytes plus data. If the length declared by the VARCHAR column is greater than 255, the length prefix is two bytes.

Well, it seems I understand a little bit. However, when the length is greater than 255, the prefix of two bytes is used. The primary subtraction question is: 65535-2 = 65533. I don't know how these Daniel calculate it. Do you still have questions?

Note: I tested UTF8 encoding. The maximum length of varchar is 21854 bytes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.