Home  >  Article  >  Database  >  What is the difference between CHAR and VARCHAR in MySQL?

What is the difference between CHAR and VARCHAR in MySQL?

王林
王林forward
2023-08-24 23:05:02911browse

MySQL 中 CHAR 和 VARCHAR 有什么区别?

CHAR and VARCHAR are both ASCII character data types and are almost identical, but they differ in the stages of storing and retrieving data from the database. Following are some important differences between CHAR and VARCHAR in MySQL-

CHAR data type VARCHAR data type
The full name is CHARACTER

The full name is VARIABLE CHARACTER

It stores a fixed-length value padded with space characters to match the specified length

VARCHAR Converts a variable-length value to a 1-byte or 2-byte length Prefix without padding any characters

can hold up to 255 characters.

Can hold up to 65,535 characters.

#It uses static memory allocation.
mysql>create table emp(name CHAR(20));
Query OK, 0 rows affected (0.25
It uses dynamic memory allocation.
mysql>create table emp1(name VARCHAR(20));
Query OK, 0 rows affected (0.21

The above is the detailed content of What is the difference between CHAR and VARCHAR in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete