先明确:bit 类型不是对应true和false,也不是只有一位
在网上能查到的比较多的关于bit的,只有单纯的bit位,而关于bit数组类型则比较少,由于工作需要,对bit数组进行了一些研究。
创建两张表:
a表为1个bit位 b表为1个64长度的bit数组位 mysql> create table a(w bit); mysql> create table b(w bit(64)); 查看a表、b表结构 mysql> desc a; +-------+--------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------+------+-----+---------+-------+ | w | bit(1) | YES | | NULL | | +-------+--------+------+-----+---------+-------+ 1 row in set (0.00 sec) mysql> desc b; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | w | bit(64) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec)
可以看出,bit类型也会自动被mysql认作是bit数组类型,不过长度为1。
在a表和b表中同一插入5个值 0 1 NULL ‘0’ ‘1’
在a表和b表中查看:
mysql> select w,bin(w) from a; +------+--------+ | w | bin(w) | +------+--------+ | | 0 | | | 1 | | NULL | NULL | | | 1 | | | 1 | +------+--------+ 5 rows in set (0.00 sec) mysql> select w,bin(w) from b; +----------+--------+ | w | bin(w) | +----------+--------+ | | 0 | | | 1 | | NULL | NULL | | 0 | 110000 | | 1 | 110001 | +----------+--------+ 5 rows in set (0.00 sec)
发现以下几个特点:
由于a表的存储情况不全,对b表进行查询。
试验情况如下:
//整数------------------------------------------- mysql> select w,bin(w) from b where w=0; +----------+--------+ | w | bin(w) | +----------+--------+ | | 0 | +----------+--------+ 1 row in set (0.00 sec) mysql> select w,bin(w) from b where w=1; +----------+--------+ | w | bin(w) | +----------+--------+ | | 1 | +----------+--------+ 1 row in set (0.00 sec) mysql> select w,bin(w) from b where w = 48; +----------+--------+ | w | bin(w) | +----------+--------+ | 0 | 110000 | +----------+--------+ 1 row in set (0.00 sec) //实数--------------------------------------------- mysql> select w,bin(w) from b where w =0.0; +----------+--------+ | w | bin(w) | +----------+--------+ | | 0 | +----------+--------+ 1 row in set (0.00 sec) mysql> select w,bin(w) from b where w =0.5; Empty set (0.00 sec) //null值------------------------------------------ mysql> select w,bin(w) from b where w is null; +------+--------+ | w | bin(w) | +------+--------+ | NULL | NULL | +------+--------+ 1 row in set (0.00 sec) //字符串------------------------------------------- mysql> select w,bin(w) from b where w='0'; +----------+--------+ | w | bin(w) | +----------+--------+ | | 0 | +----------+--------+ 1 row in set (0.00 sec) mysql> select w,bin(w) from b where w ='0.5'; Empty set (0.00 sec) mysql> select w,bin(w) from b where w ='48dsafgfgda'; +----------+--------+ | w | bin(w) | +----------+--------+ | 0 | 110000 | +----------+--------+ 1 row in set, 1 warning (0.00 sec) mysql> select w,bin(w) from b where w ='dsad'; +----------+--------+ | w | bin(w) | +----------+--------+ | | 0 | +----------+--------+ 1 row in set, 1 warning (0.00 sec)
发现以下几个特点:
这里提一下mysql的字符串转化成实数流程:
---> 字符串s
--->匹配正则表达式 `^[+-]?\d+(\.\d*)?`,获取开头的实数
bit数组类型应该作为整数类型来看,bit数组会存储整数的二进制表示,绝不是只有只能存储1位的情况。另外,对于bit类型的比较类型判断来说,还需要注意,因为char来型的缘故,所以在查询上bit=0和bit='0'其实是一样的,但是在存储上,bit存0和bit存‘0’却完全不同。