MySQL中的VARCHAR和CHAR有什么区别?
我正在尝试存储MD5哈希。
MySQL中的VARCHAR和CHAR有什么区别?
我正在尝试存储MD5哈希。
当前回答
char是一种定长字符数据类型,varchar是一种变长字符数据类型。
由于char是一种固定长度的数据类型,因此char值的存储大小等于此列的最大大小。因为varchar是一种变长数据类型,所以varchar值的存储大小是输入数据的实际长度,而不是该列的最大大小。
当期望列中的数据项具有相同的大小时,可以使用char。 当一个列中的数据条目在大小上有很大变化时,可以使用varchar。
其他回答
如果输入的字符比声明的长度短,Varchar将截断尾随空格,而char则不会。Char将填充空格,并且始终是声明的长度。在效率方面,varchar更熟练,因为它修剪字符,允许更多的调整。但是,如果您知道char的确切长度,则char将以更快的速度执行。
char是一种定长字符数据类型,varchar是一种变长字符数据类型。
由于char是一种固定长度的数据类型,因此char值的存储大小等于此列的最大大小。因为varchar是一种变长数据类型,所以varchar值的存储大小是输入数据的实际长度,而不是该列的最大大小。
当期望列中的数据项具有相同的大小时,可以使用char。 当一个列中的数据条目在大小上有很大变化时,可以使用varchar。
根据高性能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.
字符:
同时支持字符和数字。 支持2000个字符。 固定长度。
VARCHAR:
同时支持字符和数字。 支持4000字符。 可变长度。
任何评论 ......!!!!
VARCHAR是可变长度的。
CHAR是固定长度。
如果您的内容是固定大小的,那么使用CHAR将获得更好的性能。
请参阅MySQL页面上关于CHAR和VARCHAR类型的详细解释(一定要阅读注释)。