View video tutorial
MYSQL BIT
MYSQL
The MySQL BIT data type is used to store binary values of fixed length from 1 to 64 bits.
MySQL BIT
➔ The BIT data type is defined as BIT(size), where size is the number of bits per value, ranging from 1 to 64. If size is omitted, the default is BIT(1).
➔ A BIT(8) column is treated as CHAR(1) for storage internally, and a BIT(32) is treated as CHAR(4).
➔ The BIT type is unsigned by nature.
BIT(size) Range:
| Type | BIT(1) | BIT(8) | BIT(16) | BIT(32) |
|---|---|---|---|---|
| Range | 0 t 1 | 0 to 255 | 0 to 65535 | 0 to 4294967295 |
Create table:
Example
/* Drop table if the table name is already exists */
DROP TABLE IF EXISTS mytable;
/* Create Table*/
CREATE TABLE mytable(
ID int auto_increment,
NAME varchar(40),
SECTION BIT(3),
primary key (ID)
);
/* Insert values*/
INSERT INTO mytable (NAME, SECTION) VALUES ('Maria Petrov', 5);
INSERT INTO mytable (NAME, SECTION) VALUES ('Amelia Novak', b'101');
/* Query table*/
SELECT * from mytable;
Copy the code and try it out practically in your learning environment.
Insert value:
Example
/* BIT(3) decimal range 0 to 7 */
/* BIT(3) binary range 000 to 111 */
/* Insert values*/
INSERT INTO mytable (NAME, SECTION) VALUES ('Lucas Martin', 7);
/* invalid value, BIT(3) can not hold decimal 8 */
INSERT INTO mytable (NAME, SECTION) VALUES ('Alexander Müller', 8);
Copy the code and try it out in your learning environment.
Query value:
Example
/* Query all data */
SELECT * FROM mytable;
Copy the code and try it out practically in your learning environment.