我有一个非常简单的表,只有两个值:id
和type
。 type
中最长的字符串是 11 个字符(“董事会”)。
我应该使用 CHAR(11)
或CHAR(12)
在 CREATE
命令? MySQL实际上为CHAR(11)
存储了12个字节吗?其中第 12 个字节是 NULL 字符,或者这与存储无关?
编辑:正如 Joni 指出的,这不使用 11 或 12 字节,它使用 33 或 36 字节,因为我存储为 utf-8。但是,我的问题是我应该在 CHAR(x)
中使用 11 还是 12
最佳答案
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.
(来源:https://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html)
也就是说,如果您使用 ascii
或其他一些单字节编码,CHAR(M)
将以 M 字节存储。如果您使用 utf8
,它将存储在 3*M 字节中。
关于MySQL 常量字符串的最小 CHAR 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22900063/