Differences and selection principles of mysql char, varcher, and text

Source: Internet
Author: User


What are the differences between mysql char, varcher, and text? What are the differences between mysql char, varchar, and text? Let's take a look at what they have in common: both can be used to store character-type data. Both char and varchar can specify the maximum character length. text is not allowed. For example, username char (10) and www.2cto.com weibo vachar (200) are used to define a table. You can define the maximum length. Difference: The preceding statement is used as an example. username is a char-type data with a maximum length of 10 characters. If less than 10 characters are entered, spaces are automatically entered, that is to say, it will always occupy the length of 10 characters. If the content is removed, the Space following it will be automatically cleared (even if it is stored with spaces, it will be removed without spaces, such as "abcd", followed by two spaces, after the final removal, there will be no space for 'abcd') and the varchar class will become longer. For example, in the above example, the maximum length of 200 is, one or two bytes are used to record the length of the war. For example, if you store 100 characters in length, it will only occupy more than 100 characters (more than one record occupies space), rather than adding spaces to the end to occupy all the length. If there is a space after it is saved, the space will also occupy the length. After the space is removed, there will be a part of the result. These are the differences between char and varchar. Text is mainly used to store strings. It has the same length as varchar. You cannot set the default value for text. It does not support indexes well or is not supported. The speed of char is faster than that of varhcar: coordinates the relationship between space and time. Taking Weibo as an example, if www.2cto.com stores a user name, it uses char, which is faster, the occupied space is not very large, and the space usage is high. For example, if the storage space is small, some users may occupy more than 100 characters, some meager characters occupy up to 10 characters, and not every user sends only one meager one. If char is used, the loose rate is too high, the space is wasted too much, and the text speed cannot keep up, it is better to use varchar, the space is longer, and the speed is also acceptable. The current hardware prices are relatively low, so try to change the space for time to improve system efficiency and throughput. You need to analyze and consider it based on the actual situation. Author com360

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.