常量字符串的MySQL最小字符存储。

时间:2021-12-11 16:04:19

I have a very simple table that just has two values: id and type. The longest string in type is 11 characters ("Directorate").

我有一个非常简单的表,它有两个值:id和类型。最长的字符串类型是11个字符(“Directorate”)。

Should I be using CHAR(11) or CHAR(12) in the CREATE command? Does MySQL actually store 12 bytes for CHAR(11) where the 12th byte is the NULL character or is this not relevant to storage?

我是否应该在CREATE命令中使用CHAR(11)或CHAR(12) ?MySQL是否存储12字节的CHAR(11),第12字节是NULL字符,还是与存储无关?

Edit: As Joni pointed out, this does not use 11 or 12 bytes, it uses 33 or 36 bytes because I am storing as utf-8. However, my question is if I should be using 11 or 12 in CHAR(x)

编辑:正如Joni所指出的,它不使用11或12字节,它使用33或36字节,因为我存储的是utf-8。但是,我的问题是,我是否应该使用CHAR(x)中的11或12

1 个解决方案

#1


2  

The storage required for CHAR(M) is

CHAR(M)所需的存储是。

M × w bytes, 0 <= M <= 255, where w is the number of bytes required for the maximum-length character in the character set.

M×w字节,0 < = M < = 255,w是最大长度所需的字节数字符的字符集。

(Source: https://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html)

(来源:https://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html)

That is, if you use ascii or some other single byte encoding, CHAR(M) is stored in M bytes. If you use utf8 it is stored in 3*M bytes.

也就是说,如果您使用ascii或其他的单个字节编码,则CHAR(M)以M字节存储。如果使用utf8,则存储在3*M字节中。

#1


2  

The storage required for CHAR(M) is

CHAR(M)所需的存储是。

M × w bytes, 0 <= M <= 255, where w is the number of bytes required for the maximum-length character in the character set.

M×w字节,0 < = M < = 255,w是最大长度所需的字节数字符的字符集。

(Source: https://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html)

(来源:https://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html)

That is, if you use ascii or some other single byte encoding, CHAR(M) is stored in M bytes. If you use utf8 it is stored in 3*M bytes.

也就是说,如果您使用ascii或其他的单个字节编码,则CHAR(M)以M字节存储。如果使用utf8,则存储在3*M字节中。