开发者

Any benefit of uses CHAR over VARCHAR?

开发者 https://www.devze.com 2023-04-09 03:53 出处:网络
CHAR is stored as a fixed length string, VARCHAR is stored as a variable length string. I can use VARCHAR to store a fixed length string, but why people still want to use CH开发者_如何学编程AR to stor

CHAR is stored as a fixed length string, VARCHAR is stored as a variable length string. I can use VARCHAR to store a fixed length string, but why people still want to use CH开发者_如何学编程AR to store a fixed length string? Is there any benefit of using CHAR over VARCHAR? If none of benefit, why mySQL database doesn't remove the option of CHAR?


VARCHAR

VARCHAR stores variable-length character string. It can require less storage than fixed-length types because it uses only as much space as it needs.

VARCHAR also uses 1 or 2 extra bytes to record the value's length. For example varchar(10) will use up to 11 bytes of storage space. VARCHAR helps performance because it saves space. However because the rows are variable length, they can grow when you update them, which can cause extra work. if a row grows and no longer fits in its original location, the behavior is storage engine-dependent.

CHAR

CHAR is fixed-length , mysql always allocates enough space for the specified number of characters. When storing a CHAR value, MySQL removes any trailing spaces. Values are padded with spaces as needed for comparisons.

CHAR is useful if you want to store very short strings, or if all the values are nearly the same length.

For example, CHAR is a good choice for MD5 values for user passwords, which are always the same length.

CHAR is also better than VARCHAR for data that’s changed frequently, because a fixed-length row is not prone to fragmentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消