MySQL VARCHAR 和 CHAR 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1885630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:45:08  来源:igfitidea点击:

What's the difference between VARCHAR and CHAR?

sqlmysql

提问by steven

What's the difference between VARCHAR and CHAR in MySQL?

MySQL 中的 VARCHAR 和 CHAR 有什么区别?

I am trying to store MD5 hashes.

我正在尝试存储 MD5 哈希值。

采纳答案by Anon.

VARCHARis variable-length.

VARCHAR是可变长度的。

CHARis fixed length.

CHAR是固定长度。

If your content is a fixed size, you'll get better performance with CHAR.

如果您的内容是固定大小,则使用CHAR.

See the MySQL page on CHAR and VARCHAR Typesfor a detailed explanation (be sure to also read the comments).

有关详细说明,请参阅有关CHAR 和 VARCHAR 类型的 MySQL 页面(请务必阅读评论)。

回答by simplePerson43

CHAR

字符

  1. Used to store character string value of fixed length.
  2. The maximum no. of characters the data type can hold is 255 characters.
  3. It's 50% fasterthan VARCHAR.
  4. Uses static memory allocation.
  1. 用于存储固定长度的字符串值。
  2. 最大数量 数据类型可以容纳的字符数255 个字符
  3. 它比 VARCHAR快 50%
  4. 使用静态内存分配

VARCHAR

VARCHAR

  1. Used to store variable lengthalphanumeric data.
  2. The maximum this data type can hold is up to
    • Pre-MySQL 5.0.3: 255 characters.
    • Post-MySQL 5.0.3: 65,535 charactersshared for the row.
  3. It's slowerthan CHAR.
  4. Uses dynamic memory allocation.
  1. 用于存储可变长度的字母数字数据。
  2. 此数据类型最多可容纳
    • MySQL 5.0.3 之前的版本:255 个字符
    • MySQL 5.0.3 后:为该行共享65,535 个字符
  3. 它比CHAR
  4. 使用动态内存分配

回答by P Sharma

CHAR Vs VARCHAR

CHAR 与 VARCHAR

CHAR is used for Fixed Length Size Variable
VARCHAR is used for Variable Length Size Variable.

CHAR 用于固定长度大小变量
VARCHAR 用于可变长度大小变量。

E.g.

例如

Create table temp
(City CHAR(10),
Street VARCHAR(10));

Insert into temp
values('Pune','Oxford');

select length(city), length(street) from temp;

Output will be

输出将是

length(City)          Length(street)
10                    6

Conclusion: To use storage space efficiently must use VARCHAR Instead CHAR if variable length is variable

结论:要有效地使用存储空间,如果可变长度是可变的,则必须使用 VARCHAR 而不是 CHAR

回答by SLaks

A CHAR(x)column can only have exactlyxcharacters.
A VARCHAR(x)column can have up toxcharacters.

CHAR(x)列只能有完全x字符。
VARCHAR(x)列可以有多达x字符。

Since your MD5 hashes will always be the same size, you should probably use a CHAR.

由于您的 MD5 哈希值始终相同,因此您可能应该使用CHAR.

However, you shouldn't be using MD5 in the first place; it has known weaknesses.
Use SHA2 instead.
If you're hashing passwords, you should use bcrypt.

但是,您不应该首先使用 MD5;它有已知的弱点。
请改用 SHA2。
如果您要散列密码,则应使用 bcrypt。

回答by Grygoriy Gonchar

What's the difference between VARCHAR and CHAR in MySQL?

MySQL 中的 VARCHAR 和 CHAR 有什么区别?

To already given answers I would like to add that in OLTPsystems or in systems with frequent updates consider using CHAReven for variable size columns because of possible VARCHARcolumn fragmentation during updates.

对于已经给出的答案,我想补充一点,在OLTP系统或频繁更新的系统中,CHAR甚至考虑使用可变大小的列,因为VARCHAR更新期间可能存在列碎片。

I am trying to store MD5 hashes.

我正在尝试存储 MD5 哈希值。

MD5 hash is not the best choice if security really matters. However, if you will use any hash function, consider BINARYtype for it instead (e.g. MD5 will produce 16-byte hash, so BINARY(16)would be enough instead of CHAR(32)for 32 characters representing hex digits. This would save more space and be performance effective.

如果安全性真的很重要,MD5 哈希不是最佳选择。但是,如果您将使用任何散列函数,请考虑使用BINARY类型代替它(例如,MD5 将产生 16 字节的散列,因此对于表示十六进制数字的 32 个字符BINARY(16)就足够了CHAR(32)。这将节省更多空间并提高性能。

回答by user1445657

Varchar cuts off trailing spaces if the entered characters is shorter than the declared length, while char will not. Char will pad spaces and will always be the length of the declared length. In terms of efficiency, varchar is more adept as it trims characters to allow more adjustment. However, if you know the exact length of char, char will execute with a bit more speed.

如果输入的字符短于声明的长度,varchar 会切断尾随空格,而 char 不会。Char 将填充空格,并且始终是声明长度的长度。在效率方面,varchar 更擅长,因为它修剪字符以允许更多调整。但是,如果您知道 char 的确切长度,则 char 会以更快的速度执行。

回答by mobiGeek

In most RDBMSs today, they are synonyms. However for those systems that still have a distinction, a CHAR field is stored as a fixed-width column. If you define it as CHAR(10), then 10 characters are written to the table, where "padding" (typically spaces) is used to fill in any space that the data does not use up. For example, saving "bob" would be saved as ("bob"+7 spaces). A VARCHAR (variable character) column is meant to store data without wasting the extra space that a CHAR column does.

在当今大多数 RDBMS 中,它们是同义词。但是对于那些仍然有区别的系统,CHAR 字段存储为固定宽度的列。如果将其定义为 CHAR(10),则将 10 个字符写入表中,其中“填充”(通常为空格)用于填充数据未用完的任何空间。例如,保存“bob”将被保存为(“bob”+7 个空格)。VARCHAR(可变字符)列旨在存储数据而不会浪费 CHAR 列所做的额外空间。

As always, Wikipediaspeaks louder.

与往常一样,维基百科的声音更大。

回答by Andrew

CHAR is a fixed length field; VARCHAR is a variable length field. If you are storing strings with a wildly variable length such as names, then use a VARCHAR, if the length is always the same, then use a CHAR because it is slightly more size-efficient, and also slightly faster.

CHAR 是一个固定长度的字段;VARCHAR 是可变长度字段。如果您存储的字符串长度变化很大,例如名称,则使用 VARCHAR,如果长度始终相同,则使用 CHAR,因为它的大小效率更高,速度也稍快。

回答by Donnie DeBoer

CHAR is fixed length and VARCHAR is variable length. CHAR always uses the same amount of storage space per entry, while VARCHAR only uses the amount necessary to store the actual text.

CHAR 是固定长度,VARCHAR 是可变长度。CHAR 始终为每个条目使用相同数量的存储空间,而 VARCHAR 仅使用存储实际文本所需的数量。

回答by Bob Minteer

The char is a fixed-length character data type, the varchar is a variable-length character data type.

char是定长字符数据类型,varchar是变长字符数据类型。

Because char is a fixed-length data type, the storage size of the char value is equal to the maximum size for this column. Because varchar is a variable-length data type, the storage size of the varchar value is the actual length of the data entered, not the maximum size for this column.

因为 char 是固定长度的数据类型,所以 char 值的存储大小等于该列的最大大小。因为 varchar 是变长数据类型,所以 varchar 值的存储大小是输入数据的实际长度,而不是该列的最大大小。

You can use char when the data entries in a column are expected to be the same size. You can use varchar when the data entries in a column are expected to vary considerably in size.

当预计列中的数据条目大小相同时,您可以使用 char。当预计列中的数据条目大小差异很大时,您可以使用 varchar。