The main difference between CHAR(20) and VARCHAR(20) is the storage space allocation method: CHAR(20) is a fixed length, allocating 20 bytes; VARCHAR(20) is a variable length, allocating actual storage The length of the data is more efficient.
##The difference between CHAR(20) and VARCHAR(20)
CHAR(20) and VARCHAR(20) They are all character types used to store character data in MySQL. The main difference between them is how the storage space is allocated.
CHAR(20)
The fixed length is 20 characters. - Always allocate 20 bytes of space, regardless of the actual length of data stored.
- are useful for fixed-length data (such as ID numbers or postal codes) because they ensure that each field takes up the same amount of space.
-
VARCHAR(20)
Variable length, store up to 20 characters. - Only allocate space where the data is actually stored.
- Useful for data that may vary in length, such as names or addresses, as it saves storage space.
-
Other differences
CHAR(20) is padded to 20 characters, while VARCHAR(20) is not padded. - CHAR(20) can be more efficient when comparing and sorting because the length is always the same.
- VARCHAR(20) can be more efficient in terms of storage space efficiency because only the data length actually needed is allocated.
-
Summary
CHAR(20) is suitable for storing fixed-length data to ensure consistent field lengths. - VARCHAR(20) is suitable for storing data whose length may change, and the storage space is more efficient.
-
The above is the detailed content of The difference between char(20 and varchar(20) in mysql. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn