Mysql sets boolean type operations

Mysql sets boolean type operations

Mysql sets boolean type

1. Tinyint type

We create a test table and set its bl field to boolean type

create table test(
	id int PRIMARY key,
	status boolean
)

This will create success. If you look at the statement after creating the table, you will find that MySQL replaces it with tinyint(1).

insert image description here

Tinyint can only store integers from 0 to 255. Since there is no boolean type in MySQL, the tinyint[1] type is used to represent it. In MySQL, boolean=tinyint[1]

0 represents false

1 represents true

Simply define the attribute as a Boolean value in pojo: private Boolean status

When the type is tinyint[1], no matter whether it stores 0, 1, 2, 3, 4, etc., the returned value is of boolean type. When we need to use it to store integer values, we can set it to tinyint[4]

MySQL boolean type solution

1. Source of demand

Today, when defining a requirement, when using REST request to operate the database, the value corresponding to a certain key must be a boolean value.

In MySQL, a field must be a Boolean value.

{
	"neId": 2,
	"data": [{
			"alarmLevel": "1",
			"selection": true
		},
		{
			"alarmLevel": "2",
			"selection": true
		}
	]
}

2. Plundering the people’s blood and sweat

When setting boolean in MySQL, I found that there was no such type. Later, I searched "民脂民贵" and found the solution:

The boolean type is represented by tinyint.

3. Create a database

insert image description here

Standard construction structure:

DROP TABLE IF EXISTS db_msp.alarm_shield_level;
CREATE TABLE db_msp.`alarm_shield_level` (
  `neId` int(11) NOT NULL,
  `alarmLevel` varchar(32) NOT NULL,
  `alarmLevelCh` varchar(32) NOT NULL,
  `alarmLevelEn` varchar(32) NOT NULL,
  `enableStatus` tinyint(1) NOT NULL,
  PRIMARY KEY (`neId`,`alarmLevel`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

4. Conclusion

When MySQL saves Boolean values, 1 is used to represent TRUE and 0 is used to represent FALSE. The Boolean type in MySQL is tinyint(1).

When checking the library in the Java code, if the data in the library is 1, the code returns true; if the data in the library is 0, the code returns false.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Mybatis connects to MySQL database Tinyint is a boolean type detailed explanation
  • A brief discussion on several situations where MySQL returns Boolean types

<<:  A good way to improve your design skills

>>:  In-depth understanding of Linux load balancing LVS

Recommend

HTML table tag tutorial (26): cell tag

The attributes of the <TD> tag are used to ...

Introduction to MySQL Connection Control Plugin

Table of contents 1. Introduction to the connecti...

Enabling or disabling GTID mode in MySQL online

Table of contents Basic Overview Enable GTID onli...

Example of how to enable Slow query in MySQL

Preface Slow query log is a very important functi...

Why web page encoding uses utf-8 instead of gbk or gb2312?

If you have a choice, you should use UTF-8 In fac...

How to use VUE to call Ali Iconfont library online

Preface Many years ago, I was a newbie on the ser...

A guide to writing flexible, stable, high-quality HTML and CSS code standards

The Golden Rule Always follow the same set of cod...

Practice of dynamically creating dialog according to file name in vue+el-element

Table of contents background accomplish 1. Encaps...

Display flex arrangement in CSS (layout tool)

Regarding display: flex layout, some people have ...

A brief talk about JavaScript variable promotion

Table of contents Preface 1. What variables are p...

A MySQL migration plan and practical record of pitfalls

Table of contents background Solution 1: Back up ...

How to monitor global variables in WeChat applet

I recently encountered a problem at work. There i...

Problems with Vue imitating Bibibili's homepage

Engineering Structure The project is divided into...