Differences between char and varchar in mysql

Source: Internet
Author: User

In mysql, the difference between char and varchar is used to store strings, but their storage method is different. char has a fixed length, and varchar belongs to the variable length character type.

InDifferences between char and varchar In the mysql tutorialThey are all used to store strings, but their storage methods are different. char has a fixed length, and varchar is a variable-length character type.

Differences between char and varchar

Char (13) is fixed in length. For example, the storage of 'www. bKjia. c0m' requires 13 characters.

Varchar (13) Variable Length: for example, 'www. bKjia. c0m' requires 14 characters in storage space,

From the above we can see that the char length is fixed, no matter how much data you store, it will be fixed length. WhileVarcharThe variable length is added to the total length, which is used to store the location. Therefore, in actual applications, users can do this based on their own data types.

Let's take a look.Char, the difference in speed with varcharRight.

Mysal> create tabe AB (v varchar (4), c char (4 ));

Query OK, 0 rows affected (0.02 sec)

Mysql> insert into abc values ('AB', 'AB ')

Query OK, 1 row affected (0.00 sec );

Mysql-> select concat (v, '+'), concat (c, '+') form abc

AB + | AB +

1 rows in set (0.00 sec)

It can be seen from the above that, for some reason, the char is fixed length, so the processing speed is much faster than that of varchar, but it is not suitable for storage space, however, if the speed requirement is met, the char type can be used. Otherwise, the varchar type can be used for instances.

Note:

When the char character type is used, the content must be followed by space. Otherwise, spaces will be automatically deleted.

Purpose:

We recommend that you use a fixed length for the myisam storage engine. Data columns replace variable-length data columns.

The memory storage engine currently uses fixed data Row Storage, so no matter whether the char varchar column is used,

The innodb Storage engine is intended to use the varchar type

Now, the original tutorial on this site is reprinted with the source www. bKjia. c0m/database/database.html.

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.