当前位置:网站首页>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.
边栏推荐
- [today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
- Win10 or win11 taskbar, automatically hidden and transparent
- Ctfshow, information collection: web12
- CTFshow,信息搜集:web1
- Compile advanced notes
- Ctfshow, information collection: web13
- [Yugong series] go teaching course 005 variables in July 2022
- Promoted to P8 successfully in the first half of the year, and bought a villa!
- IDA pro逆向工具寻找socket server的IP和port
- Why do we use UTF-8 encoding?
猜你喜欢
Niuke real problem programming - day20
asp.netNBA信息管理系统VS开发sqlserver数据库web结构c#编程计算机网页源码项目详细设计
In the field of software engineering, we have been doing scientific research for ten years!
什麼是數據泄露
Win10 or win11 taskbar, automatically hidden and transparent
Niuke real problem programming - day18
Cocoscreator operates spine for animation fusion
Ctfshow, information collection: web12
【OBS】RTMPSockBuf_ Fill, remote host closed connection.
JSON解析实例(Qt含源码)
随机推荐
Niuke real problem programming - day20
Why do we use UTF-8 encoding?
什么是数据泄露
Stream learning notes
数据库如何进行动态自定义排序?
PAT 甲级 1103 Integer Factorizatio
Shengteng experience officer Episode 5 notes I
Navigation - are you sure you want to take a look at such an easy-to-use navigation framework?
Cocoscreator operates spine for animation fusion
6. Electron borderless window and transparent window lock mode setting window icon
Full details of efficientnet model
Yyds dry goods inventory # solve the real problem of famous enterprises: cross line
Delete a whole page in word
Ctfshow, information collection: web14
Win10 or win11 taskbar, automatically hidden and transparent
时空可变形卷积用于压缩视频质量增强(STDF)
Ctfshow, information collection: web10
Unity之ASE实现全屏风沙效果
Introduction and use of Kitti dataset
Bye, Dachang! I'm going to the factory today