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.