当前位置:网站首页>MySQL bit type resolution
MySQL bit type resolution
2022-07-07 15:07:00 【Kevin Cui】
BIT Type introduction
BIT Data types are used to store bit values . Its data has two values :0 and 1, This data type is often used as a logical variable , Used to express true 、 False or yes 、 No, wait for binary selection . More like the most primitive computer language programming .
stay MySQL in BIT Summarized as Numeric Data type .BIT The data type stores bit values , Support MyISAM、MEMORY、InnoDB、NDB surface .
In data types ,bit It should occupy the smallest space .

BIT(M) Type allows storage M A value .M The value range is 1 ~ 64.NDB All in the cluster BIT The maximum sum of columns cannot exceed 4096 position
If you assign a value with a length less than M Bit BIT(M) Column , Then the value will be filled in on the left 0.
for example : to BIT(6) Column assignment b’101’ In fact, it is equivalent to giving b’000101’ assignment .
that BIT What environment is suitable for the scene , In the real world , A typical case , attendance .“1” Show attendance ,“0” Indicate absence , This makes it easier to identify the days when everyone is present or absent .
MySQL> DROP TABLE IF EXISTS attendance;
MySQL> CREATE TABLE attendance (
emp_no CHAR(3),
emp_name CHAR(50),
attend BIT(5),
class INT,
KEY `idx_bit` (`attend`)
);
# Use b'val' The way of writing , Val It's using 0 and 1 Written binary values
MySQL> INSERT INTO attendance (emp_no, emp_name, attend, class)
VALUES ('001','Jim',b'11111',5),('002','Kim',b'11000',5),('003','Cui',b'00111',5),
('004','King',b'11101',5),('005','Wang',b'101',5),('006','Chen',NULL,5),
('007','Piao',0,5),('008','Hu',1,5);
MySQL> SELECT emp_no,emp_name,attend ,class FROM attendance;
+--------+----------+----------------+-------+
| emp_no | emp_name | attend | class |
+--------+----------+----------------+-------+
| 001 | Jim | 0x1F | 5 |
| 002 | Kim | 0x18 | 5 |
| 003 | Cui | 0x07 | 5 |
| 004 | King | 0x1D | 5 |
| 005 | Wang | 0x05 | 5 |
| 006 | Chen | NULL | 5 |
| 007 | Piao | 0x00 | 5 |
| 008 | Hu | 0x01 | 5 |
+--------+----------+----------------+-------+
8 rows in set (0.00 sec)
#BIN Way to show
MySQL> SELECT emp_no,emp_name,BIN(attend) ,class FROM attendance;
+--------+----------+-------------+-------+
| emp_no | emp_name | BIN(attend) | class |
+--------+----------+-------------+-------+
| 001 | Jim | 11111 | 5 |
| 002 | Kim | 11000 | 5 |
| 003 | Cui | 111 | 5 |
| 004 | King | 11101 | 5 |
| 005 | Wang | 101 | 5 |
| 006 | Chen | NULL | 5 |
| 007 | Piao | 0 | 5 |
| 008 | Hu | 1 | 5 |
+--------+----------+-------------+-------+
8 rows in set (0.00 sec)
# String left padding function LPAD
MySQL> SELECT emp_no,emp_name,LPAD(BIN(attend),5,0) ,class FROM attendance;
+--------+----------+-----------------------+-------+
| emp_no | emp_name | LPAD(BIN(attend),5,0) | class |
+--------+----------+-----------------------+-------+
| 001 | Jim | 11111 | 5 |
| 002 | Kim | 11000 | 5 |
| 003 | Cui | 00111 | 5 |
| 004 | King | 11101 | 5 |
| 005 | Wang | 00101 | 5 |
| 006 | Chen | NULL | 5 |
| 007 | Piao | 00000 | 5 |
| 008 | Hu | 00001 | 5 |
+--------+----------+-----------------------+-------+
8 rows in set (0.00 sec) remarks :
You can use bit valued functions , And you can use decimal 、 Binary or any other data conversion function retrieves bit values . Use LPAD and BIN Function to retrieve data in the appropriate format .
Data query :
Corresponding WHERE Conditions bit Field must be b’val’ Or int type
##1. use INT Type number to query
MySQL> SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class
FROM attendance where attend=7;
+--------+----------+-------------+-------------+----------------+-------+
| emp_no | emp_name | BIN(attend) | HEX(attend) | attend | class |
+--------+----------+-------------+-------------+----------------+-------+
| 003 | Cui | 111 | 7 | 0x07 | 5 |
+--------+----------+-------------+-------------+----------------+-------+
##2. Query by location
MySQL> SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class
FROM attendance where attend=b'111';
+--------+----------+-------------+-------------+----------------+-------+
| emp_no | emp_name | BIN(attend) | HEX(attend) | attend | class |
+--------+----------+-------------+-------------+----------------+-------+
| 003 | Cui | 111 | 7 | 0x07 | 5 |
+--------+----------+-------------+-------------+----------------+-------+
##3.string Type to query
MySQL> SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class
FROM attendance where attend='7';
Empty set (0.00 sec)
##4.in The statement is partially invalid ( character string )
MySQL> SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class
FROM attendance WHERE attend in('7',5);
+--------+----------+-------------+-------------+----------------+-------+
| emp_no | emp_name | BIN(attend) | HEX(attend) | attend | class |
+--------+----------+-------------+-------------+----------------+-------+
| 003 | Cui | 111 | 7 | 0x07 | 5 |
+--------+----------+-------------+-------------+----------------+-------+
##5. NULL A field
MySQL> SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class
FROM attendance WHERE attend IS NULL;
+--------+----------+-------------+-------------+----------------+-------+
| emp_no | emp_name | BIN(attend) | HEX(attend) | attend | class |
+--------+----------+-------------+-------------+----------------+-------+
| 006 | Chen | NULL | NULL | NULL | 5 |
+--------+----------+-------------+-------------+----------------+-------+
1 row in set (0.00 sec) remarks :bit Fields can only be integer types or bit Type can match . Within the range of numbers b’val’ Or The integer type corresponds to the equivalent .
such as , An example of an appeal 7 and b’111’ Is equivalent . about NULL value ,bit The type is still equal to null value
Indexes
Following pair bit The field exists under the index , As a condition , Whether the index can be used .
Such as equivalent query , Range queries , type (int,bin,string)
# Numeric type
MySQL> EXPLAIN SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class
FROM attendance where attend=7;
+----+-------------+------------+------------+------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | attendance | NULL | ref | idx_bit | idx_bit | 2 | const | 1 | 100.00 | NULL |
+----+-------------+------------+------------+------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
##2. Query by location
MySQL> EXPLAIN SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class FROM attendance where attend=b'111';
+----+-------------+------------+------------+------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | attendance | NULL | ref | idx_bit | idx_bit | 2 | const | 1 | 100.00 | NULL |
+----+-------------+------------+------------+------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
##3. Range
MySQL> EXPLAIN SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class
FROM attendance WHERE attend>17;
+----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | attendance | NULL | range | idx_bit | idx_bit | 2 | NULL | 3 | 100.00 | Using index condition |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)
MySQL> EXPLAIN SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class
FROM attendance WHERE attend>b'111';
+----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | attendance | NULL | range | idx_bit | idx_bit | 2 | NULL | 3 | 100.00 | Using index condition |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.01 sec)
##5. use string The type field
MySQL> EXPLAIN SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class
FROM attendance WHERE attend='7';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | no matching row in const table |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
1 row in set, 1 warning (0.00 sec)remarks : Index only accepts int , b’val’ type
BIT Functions and operators
MySQL 8.0 in , Bit functions and operators allow binary string type parameters (binary, VARBINARY and BLOB type ). This makes it possible to produce more than 64 Return value of bit , Easier to perform bit operations . Bit functions and operators include BIT_COUNT()、BIT_AND()、BIT_OR()、BIT_XOR()、&、|、^、~、<< and >>.

Be careful :MySQL 8.0 Bit operations on binary string parameters in may result in 5.7 Different results . therefore MySQL 5.7 and 8.0 There may be incompatible information between .
#BIT_COUNT()
MySQL> SELECT emp_no,emp_name,BIN(attend),HEX(attend),attend ,class,bit_count(attend) FROM attendance;
+--------+----------+-------------+-------------+----------------+-------+-------------------+
| emp_no | emp_name | BIN(attend) | HEX(attend) | attend | class | bit_count(attend) |
+--------+----------+-------------+-------------+----------------+-------+-------------------+
| 001 | Jim | 11111 | 1F | 0x1F | 5 | 5 |
| 002 | Kim | 11000 | 18 | 0x18 | 5 | 2 |
| 003 | Cui | 111 | 7 | 0x07 | 5 | 3 |
| 004 | King | 11101 | 1D | 0x1D | 5 | 4 |
| 005 | Wang | 101 | 5 | 0x05 | 5 | 2 |
| 006 | Chen | NULL | NULL | NULL | 5 | NULL |
| 007 | Piao | 0 | 0 | 0x00 | 5 | 0 |
| 008 | Hu | 1 | 1 | 0x01 | 5 | 1 |
+--------+----------+-------------+-------------+----------------+-------+-------------------+
8 rows in set (0.00 sec)
#UUID and IPV6 Some of the transformations , Because it contains some “-”,“::” The symbol of
MySQL> SELECT HEX(UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db'));
+----------------------------------------------------------+
| HEX(UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db')) |
+----------------------------------------------------------+
| 6CCD780CBABA102695645B8C656024DB |
+----------------------------------------------------------+
#IPV6
MySQL> SELECT HEX(INET6_ATON('fe80::219:d1ff:fe91:1a72'));
+---------------------------------------------+
| HEX(INET6_ATON('fe80::219:d1ff:fe91:1a72')) |
+---------------------------------------------+
| FE800000000000000219D1FFFE911A72 |
+---------------------------------------------+summary
stay MySQL According to the current for bit The understanding of the
- Use scenarios , Attendance is similar to the scenario + BIT_COUNT Statistics .
- bit The most practical function is UUID ,IPV6 To deal with .
- Data length and storage , Generally, the minimum length ,0 and 1 Combine the underlying data structures .
- In terms of index construction, it still follows the integer type Btree.
边栏推荐
- "Baidu Cup" CTF competition 2017 February, web:include
- What is cloud primordial? This time, I can finally understand!
- Navigation - are you sure you want to take a look at such an easy-to-use navigation framework?
- Protection strategy of server area based on Firewall
- Ctfshow, information collection: web13
- 15、文本编辑工具VIM使用
- Ctfshow, information collection: web10
- JSON解析实例(Qt含源码)
- Pat grade a 1103 integer factorizatio
- MySQL bit类型解析
猜你喜欢

How bad can a programmer be? Nima, they are all talents

Ctfshow, information collection: web9

#yyds干货盘点# 解决名企真题:交叉线

CTFshow,信息搜集:web5

Jetson AGX Orin CANFD 使用

【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例

【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例

CTFshow,信息搜集:web12

13 ux/ui/ue best creative inspiration websites in 2022

用于增强压缩视频质量的可变形卷积密集网络
随机推荐
拜拜了,大厂!今天我就要去厂里
Lidar Knowledge Drop
简述keepalived工作原理
Summer safety is very important! Emergency safety education enters kindergarten
Comparable and comparator of sorting
Webrtc audio anti weak network technology (Part 1)
Niuke real problem programming - Day17
Ctfshow, information collection: web4
Es log error appreciation -trying to create too many buckets
CTFshow,信息搜集:web1
How bad can a programmer be? Nima, they are all talents
安恒堡垒机如何启用Radius双因素/双因子(2FA)身份认证
CTFshow,信息搜集:web13
How does the database perform dynamic custom sorting?
Several ways of JS jump link
[server data recovery] a case of RAID data recovery of a brand StorageWorks server
【深度学习】图像超分实验:SRCNN/FSRCNN
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
@Introduction and three usages of controlleradvice
CTFshow,信息搜集:web9