Skip to content


关于 MYSQL 字符串类型的存储需求

关于mysql中的列类型如何定义,是一个让初学者让人头痛的问题。列类型的恰当定义是一个数据库高效存储的重要保证,其中字符串类型的定义是最重要也是相对较难的。很不幸到至今我才清楚char,varchar的真正定义方式 。
原因有二.
1.没有亲自动手去试错,文档上说的怎么样,就天真的认为就是那样
2.是看过官方源文档,但没有查看官方原版英文文档

咱们通过对比来加深记忆

1.mysq中文文档http://dev.mysql.com/doc/refman/5.1/zh/column-types.html#storage-requirementsen_Storage_Requirements_for_String

2. mysql英文文档http://dev.mysql.com/doc/refman/5.1-maria/en/storage-requirements.html

cn_Storage_Requirements_for_String

对比上面的两张表中char与varchar的描述

char:

中文版: char(M)     M个字节,0 <= M <= 255

英文版:  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表示的字节数,也就是char(M)表示存储M个字节,英文版表明M表示字符数。实际存储的大小根据字符集字节大小决定.实验证明英文版是正确的.

varchar:

中文版: VARCHAR(M) L+1个字节,其中L <= M 且0 <= M <= 65535(参见下面的注释)

英文版: VARCHAR(M) VARBINARY(M) L + 1 bytes if column values require 0 – 255 bytes, L + 2 bytes if values may require more than 255 bytes

==>中文版中的L<=M是不对的,当保存的是unicode字符的时间L可能大于M.   英文版中表示当大小大于255字节之后字节大小是L+2. 英文版中明确表示: L represents the actual length in bytes of a given string value.

综上所述: M表示字符数,中文文档中说明 M表示字节数是存在问题的.


Posted in mysql.

Tagged with .


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. 项链 says

    最近博主都发专业文章,我还是喜欢看感悟,呵呵



Some HTML is OK

or, reply to this post via trackback.