The difference between char and varchar in MySQL

Source: Internet
Author: User
Tags mysql tutorial

The difference between char and varchar in the MySQL tutorial is used to store strings, except that they are stored in a different way, Char has a fixed length, and varchar is a variable-length character type.

The difference between char and varchar

char (13) length fixed, such as ' www.111cn.net ' storage requires space 13 characters

varchar (13) variable length as ' www.111cn.net ' requires storage space of 14 characters,

From the above you can see that the char length is fixed, no matter how much data you store it will be fixed length. The varchar is a variable length but he wants to add 1 characters to the total length, which is used to store the location. So the actual application of the user can be based on their own data types to do.

And look at Char, the difference between speed and varchar .

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+

1rows in Set (0.00 sec)

As can be seen from the above, for some reason char fixed length, so in processing speed than varchar a lot faster, but the cost of storage space, so the storage is small, but the speed of the requirements can use the char type, conversely can be used varchar type to instance.

Indicate:

When a char character type is used, the content must be processed when there is space behind it, or the space will be automatically deleted.

Jian Yi:

The MyISAM storage engine recommends a fixed length, with data columns instead of variable-length data columns.

The memory storage engine currently uses fixed data row storage, so it doesn't matter whether you use a char varchar column,

InnoDB storage Engine is intended to use varchar type

All right, this site original tutorial reproduced annotated source www.111cn.net/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.