Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference and selection principle of mysql char,varcher,text?

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about the differences and selection principles of mysql char,varcher,text. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

The difference and selection principle of char,varcher,text

What is the difference in the use of char,varchar,text in mysql?

Let's take a look at what they have in common: both can be used to store data of character types.

Char,varchar can specify the maximum character length, but text cannot.

such as,

Username char (10)

Www.2cto.com

Weibo vachar (200)

This is used when defining tables. Can define the maximum length.

Difference: take the above statement as an example. Username is char type data, and the maximum character length is 10 characters. If you enter less than 10 characters, spaces will be filled automatically, that is, the length of 10 characters will always be occupied. If the content is taken out, the following spaces will be cleared automatically (even if the storage is followed by spaces, the spaces will not be removed after the storage, for example, "abcd", followed by two spaces, and there will be no spaces after the final removal).

The varchar class is longer, such as the example above, with a maximum length of 200, of which one or two bytes will be used to record the war length. For example, if you save 100 characters, you will end up with a little more than 100 characters (more than a series of records take up space), rather than adding spaces to take up all the length. If there is a space after it is saved, then the space after the space will also take up the length, and the space will be taken out as part of the result. these are the differences between char and varchar.

Text mainly stores strings, which is the same length as varchar. Text cannot set default values, and the support for indexes is not very good or not.

Char is faster than varhcar.

Principles of use:

Coordinate the relationship between space and time

Take Weibo as an example

Www.2cto.com

If the user name is stored, use char, which is fast, takes up less space, and has a high space utilization rate, such as a maximum of 10 characters.

If the storage is thin, some users will occupy more than 100 characters, while others will occupy up to 10 characters, and not only one message per user. If you use char, the waste rate is too high, the space waste is too big, and the speed of using text can not keep up. It is better to use varchar, the space is longer, and the speed is OK.

Now the price of hardware is relatively cheap, try to let space for time, improve the efficiency and throughput of the system, but also to achieve a certain degree. It needs to be analyzed and considered according to the specific situation.

These are the differences and selection principles of mysql char,varcher,text shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 233

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report