当前位置:网站首页>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.
边栏推荐
- 激光雷达lidar知识点滴
- [机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
- What is data leakage
- MySQL installation configuration 2021 in Windows Environment
- CTFshow,信息搜集:web12
- Niuke real problem programming - day15
- Niuke real problem programming - day13
- 一个需求温习到的所有知识,h5的表单被键盘遮挡,事件代理,事件委托
- Ctfshow, information collection: web13
- C 6.0 language specification approved
猜你喜欢
[机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
Mathematical modeling -- what is mathematical modeling
Ctfshow, information collection: web8
Spatiotemporal deformable convolution for compressed video quality enhancement (STDF)
Webrtc audio anti weak network technology (Part 1)
IDA pro逆向工具寻找socket server的IP和port
Five pain points for big companies to open source
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
What is the process of ⼀ objects from loading into JVM to being cleared by GC?
8大模块、40个思维模型,打破思维桎梏,满足你工作不同阶段、场景的思维需求,赶紧收藏慢慢学
随机推荐
Summer safety is very important! Emergency safety education enters kindergarten
连接ftp服务器教程
CTFshow,信息搜集:web10
Guangzhou Development Zone enables geographical indication products to help rural revitalization
Webrtc audio anti weak network technology (Part 1)
一文读懂数仓中的pg_stat
IDA pro逆向工具寻找socket server的IP和port
【OBS】RTMPSockBuf_ Fill, remote host closed connection.
What are PV and UV? pv、uv
word中删除一整页
STM32F103C8T6 PWM驱动舵机(SG90)
Cocoscreator resource encryption and decryption
With 8 modules and 40 thinking models, you can break the shackles of thinking and meet the thinking needs of different stages and scenes of your work. Collect it quickly and learn it slowly
Infinite innovation in cloud "vision" | the 2022 Alibaba cloud live summit was officially launched
In the field of software engineering, we have been doing scientific research for ten years!
Compile advanced notes
Today's sleep quality record 78 points
【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
Niuke real problem programming - day18
知否|两大风控最重要指标与客群好坏的关系分析