您的位置:首页 > 数据库 > MySQL

mysql协议学习(一)——基本数据类型

2015-07-30 23:19 691 查看
mysql协议基本数据类型分为两大类

一、Integers

(1)Fixed-length integers 定长整形

定长整形的值直接存在字节中,分别有int<1>、int<2>、int<3>、int<4>、int<6>、int<8>6种字节类型

eg: int<3>用 01 00 00 三个字节表示数值1

(2)Length-encoded integers (integer<lenenc>)长度编码整形

长度编码整形也有几种字节存储类型,分别有1,3,4,5共4种存储方式,使用字节的多少取决于需要存储的数值的大小

If the value is < 251, it is stored as a 1-byte integer. // 当存储的值小于251时,存储为1个字节,该字节直接存放数据的值

If the value is ≥ 251 and < (216), it is stored as
fc
+ 2-byte integer. // 第一个字节固定为0xfc(十六进制表示),另外两个字节存放数据的值

If the value is ≥ (216) and < (224), it is stored as
fd
+ 3-byte integer. // 第一个字节固定为0xfd,另外三个字节存放数据的值

If the value is ≥ (224) and < (264) it is stored as
fe
+ 4-byte integer. // 第一个字节固定为0xfe,另外四个字节存放数据的值

eg:
fa -- 250fc fb 00 -- 251

二、Strings

字符串基本类型在mysql协议中的使用率是最高的,特别在Text Protocol协议中使用广泛。

(1)FixedLengthString(string<fix>)固定长度字符串

ERR_Packet中sql_state字段就是使用了固定5个字节长度的字符串来表示

(2)NulTerminatedString (string<NUL>)以字节[00]为结束夫的字符串

(3)VariableLengthString (string<var>)变长字符串

(4)LengthEncodedString (string<lenenc>)长度编码字符串,该字符串由length + string两部分组成,其中length指定string所占用的字节数,length用Length-encoded integers来表示,而string在名义上就是FixedLengthString了

(5)RestOfPacketString(string<EOF>) 用于Packet的最后一部分,长度为整个Packet的大小减去当前已经读到的字节大小

学习参考http://dev.mysql.com/doc/internals/en/basic-types.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: