MySQL中的VARCHAR和CHAR有什么区别?
我正在尝试存储MD5哈希。
MySQL中的VARCHAR和CHAR有什么区别?
我正在尝试存储MD5哈希。
当前回答
根据高性能MySQL书:
VARCHAR stores variable-length character strings and is the most common string data type. It can require less storage space than fixed-length types, because it uses only as much space as it needs (i.e., less space is used to store shorter values). The exception is a MyISAM table created with ROW_FORMAT=FIXED, which uses a fixed amount of space on disk for each row and can thus waste space. VARCHAR helps performance because it saves space. 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. (This was also true of VARCHAR in MySQL 4.1 and older versions—CHAR and VAR CHAR were logically identical and differed only in storage format.) Values are padded with spaces as needed for comparisons.
其他回答
根据高性能MySQL书:
VARCHAR stores variable-length character strings and is the most common string data type. It can require less storage space than fixed-length types, because it uses only as much space as it needs (i.e., less space is used to store shorter values). The exception is a MyISAM table created with ROW_FORMAT=FIXED, which uses a fixed amount of space on disk for each row and can thus waste space. VARCHAR helps performance because it saves space. 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. (This was also true of VARCHAR in MySQL 4.1 and older versions—CHAR and VAR CHAR were logically identical and differed only in storage format.) Values are padded with spaces as needed for comparisons.
char是一种定长字符数据类型,varchar是一种变长字符数据类型。
由于char是一种固定长度的数据类型,因此char值的存储大小等于此列的最大大小。因为varchar是一种变长数据类型,所以varchar值的存储大小是输入数据的实际长度,而不是该列的最大大小。
当期望列中的数据项具有相同的大小时,可以使用char。 当一个列中的数据条目在大小上有很大变化时,可以使用varchar。
CHAR是一个固定长度的字段;VARCHAR是一个变长字段。如果您存储的字符串长度变化很大,例如名称,则使用VARCHAR,如果长度总是相同,则使用CHAR,因为它的大小效率略高,也略快。
区分这两者对完整性方面也有好处。
如果你希望存储有长度规则的东西,比如yes或no,那么你可以使用char(1)来存储Y或n。对于货币代码也很有用,你可以使用char(3)来存储像USD, EUR或AUD这样的东西。
varchar更适合那些除了限制长度之外没有一般规则的情况。它适用于名称或描述之类的东西,在这些地方,值的长度会有很多变化。
然后出现了文本数据类型,并在工作中使用了扳手(尽管它通常只是没有定义上限的varchar)。
字符:
同时支持字符和数字。 支持2000个字符。 固定长度。
VARCHAR:
同时支持字符和数字。 支持4000字符。 可变长度。
任何评论 ......!!!!