Introduction to MySQL BIT
BIT is a data type used in MySQL. This type stores bit values within range of 1-64. It is generally defined in the create table or defining statements and denoted as ‘BIT(n)’, where ‘n’ is the number of bit values that can be stored. This ‘n’ value keeps the range from 1-64 and if not defined in the statement, it is defaulted to 1 bit value. This bit value will be storing binary values. We know in MySQL, we need to call a binary value as ‘BIN()’ to be displayed as a binary value. We will discuss further on this data type below.
The BIT data type is defined as below:
Here, the keyword BIT defines it to store binary values as bits and the bit value is mentioned in variable ‘n’. When called upon, to be displayed as a binary value, the BIT data type will have syntax as below. And if otherwise, the corresponding decimal value will be returned.
BIN ( BIT (n))
Let’s have look at the usage and examples of BIT data type.
How Does BIT Data Type work in MySQL?
BIT (n) is defined with a defining statement like ‘create table’ etc. Please note that, if the value ‘n’ is not specified, the BIT value is defaulted to ‘1’ bit. We can create a table to understand the BIT data type. Our table is to store the attendance of students in a class for 5 working days. The table holds data for student name, attendance as BIT value in binary with ‘1’ denoting presence & ‘0’ denoting absence and the class to which student belongs. The create table statement is as below:
CREATE TABLE attendance (
So the table name is attendance with three columns. Name of the student is stored as char data type, class in the INT data type and the attendance is stored as a BIT data type. The attendance filed will have 5 values representing the 5 working days.
Let’s insert values into these fields.
INSERT INTO attendance (STUDENT_NAME, ATTENDANCE, CLASS)
A BIT value is inserted as a bit literal. Bit value literals are defined using b’val’ or ‘0bval’ notations.
The table has now four rows with data for 4 different student attendance.
We will retrieve the data with the SELECT query.
select * from attendance;
The simple SELECT query to retrieve complete data from table attendance.
The output retrieved is not showing correct data or data in our desired manner. We had input the attendance for 5 days from Monday to Friday for each student. But while retrieving, in the SELECT query, we have not specified how the bit value is to be displayed. Thus the value is displayed as a decimal value corresponding to the binary input shared.
Now, let’s try retrieving the data as binary values itself. For that, we need to specifically call the column attendance as BIN().
SELECT STUDENT_NAME, BIN(ATTENDANCE), CLASS FROM attendance;
The BIN() function will display the binary values input as bit value literals in the binary format itself.
We now get the attendance of each student as ‘1’ and ‘0 ‘, thus making it easier to identify the days when each of them were present or absent. But Anna’s attendance is having a slight issue here. She was absent on Monday and Tuesday. So that was marked as ‘0’ for both days. Since the bit value literal started with zeros, it omitted the preceding zeros while displaying the output. This can cause confusion as the data is available only for 3 days out of a total 5 days.
To avoid this situation, we can use the LPAD function along with the BIN function.
SELECT STUDENT_NAME, LPAD(BIN(ATTENDANCE),5,'0') as ATTENDANCE, CLASS FROM attendance;
Now all attendance look similar as the count of days is constantly 5 days.
These were several methods of retrieving a BIT data type in MySQL. In our variable definition statement, we had specified the number of bits in the variable attendance as 5, by the part of the query – attendance BIT(5). As discussed, this count can range from 1-64 and the default count is 1 when no digit is specified within ().
We can see some examples of different lengths in the BIT value.
CREATE TABLE sampleBITs (
A table with 3 variables sample1, sample2 and sample3 are created. Sample2 has no length specified, which means the length is 1. We can insert the values into this table.
INSERT INTO sampleBITs (sample1, sample2, sample3)
VALUES (b'1010101010', 0b1, B'10001100011000110001111111000110001100011100110001');
select * from sampleBITs;
Note that, here we have inserted values using all 3 valid bit value laterals.
Now, the table can be retrieved as below.
LPAD(BIN(sample1),10,'0') as sample1,
LPAD(BIN(sample2),1,'0') as sample2,
LPAD(BIN(sample3),50,'0') as sample3
We have used LPAD and BIN functions to retrieve the data in a proper format.
So as we discussed, the BIT data type is used to store bit values and is defined as BIT(n) where ‘n’ specifies the count of bits within the variable. The value of ‘n’ can be within ‘1’ to ‘64’. To input data into the bit variable, we can use bit value literals and the same can be retrieved as decimal, binary or any other data conversion functions.
This is a guide to MySQL BIT. Here we discuss the Introduction to MySQL BIT and the practical examples and different subquery expressions. You can also go through our suggested articles to learn more –