Cara menggunakan mysql bit data type

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The BIT data type is defined as below:

All in One Data Science Bundle(360+ Courses, 50+ projects)

Cara menggunakan mysql bit data type
Cara menggunakan mysql bit data type
Cara menggunakan mysql bit data type
Cara menggunakan mysql bit data type

Cara menggunakan mysql bit data type
Cara menggunakan mysql bit data type
Cara menggunakan mysql bit data type
Cara menggunakan mysql bit data type

Price
View Courses

360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,060 ratings)

BIT (n);

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:

Query:

CREATE TABLE attendance (
STUDENT_NAME CHAR(50),
ATTENDANCE BIT(5),
CLASS INT
);

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.

Query:

INSERT INTO attendance (STUDENT_NAME, ATTENDANCE, CLASS)
VALUES ('ALAN',b'11111',5),
('EMIL',b'11000',5),
('ANNA',b'00111',5),
('JOHN',b'11101',5);

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.

Query:

select * from attendance;

The simple SELECT query to retrieve complete data from table attendance.

Output:

Cara menggunakan mysql bit data type

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().

Query:

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.

Output:

Cara menggunakan mysql bit data type

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.

Query:

SELECT STUDENT_NAME, LPAD(BIN(ATTENDANCE),5,'0') as ATTENDANCE, CLASS FROM attendance;

Output:

Cara menggunakan mysql bit data type

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.

Query:

CREATE TABLE sampleBITs (
sample1 BIT(10),
sample2 BIT,
sample3 BIT(50));

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.

Query:

INSERT INTO sampleBITs (sample1, sample2, sample3)
VALUES (b'1010101010', 0b1, B'10001100011000110001111111000110001100011100110001');
select * from sampleBITs;

Output:

Cara menggunakan mysql bit data type

Note that, here we have inserted values using all 3 valid bit value laterals.

Now, the table can be retrieved as below.

Query:

SELECT
LPAD(BIN(sample1),10,'0') as sample1,
LPAD(BIN(sample2),1,'0') as sample2,
LPAD(BIN(sample3),50,'0') as sample3
FROM sampleBITs;

Output:

Cara menggunakan mysql bit data type

We have used LPAD and BIN functions to retrieve the data in a proper format.

NOTE: Bit literals can be specified as b’val’ or 0bval. The b’val’ can also be denoted as B’val’, as the literal is not case sensitive in this format. But the leading 0b is case sensitive and cannot be replaced with 0B. A bit-value literal is always binary value by default, in both notations.

Conclusion

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 –

MySQL menggunakan model database jenis apa?

1. Pengertian MySQL. MySQL merupakan sistem manajemen database relasional (RDBMS) yang bersifat open source berbasis SQL dan bekerja dengan model client-server. MySQL sering juga disebut dengan my sequel.

Bagaimana cara membuat database di MySQL?

Untuk membuat database MySQL baru, masuk cPanel dan klik menu MySQL Databases:.
Pilih nama database. Prefix defaultnya adalah "yoururser_". ... .
Scroll ke bawah dan cari Add New User. Masukkan username dan password, lalu klik Create User:.
Cari opsi Add User To Database untuk menghubungkan User ke Database. ... .
Selesai!.

Langkah kerja MySQL?

Cara kerja MySQL.
MySQL membuat database yang dapat memodifikasi, menyimpan data, dan menentukan keterkaitan tabel-tabel yang ada di dalam software..
Kemudian, perangkat pengguna membuat request dengan perintah spesifik menggunakan bahasa SQL..
Terakhir, server akan menerima dan menjalankan perintah..

Langkah awal membuat database MySQL?

Yuk mulai dari langkah pertama cara buat database MySQL!.
Download dan Install XAMPP. ... .
2. Aktifkan Apache dan MySQL di XAMPP. ... .
3. Akses localhost/phpMyAdmin di Browser. ... .
4. Buat Database MySQL di phpMyAdmin. ... .
Buat Tabel Baru di Database MySQL. ... .
6. Input Data di Tabel Database MySQL. ... .
7. Masukkan Project Website di localhost..