MySQL's Boolean and tinyint (1)

Source: Internet
Author: User

Boolean type
MySQL saves a Boolean value with 1 for true,0 representing False,boolean in MySQL with type tinyint (1),
There are four constants in MySQL: True,false,true,false, which represent 1,0,1,0 respectively,
Mysql> Select True,false,true,false;
+------+-------+------+-------+
| TRUE | FALSE | TRUE | FALSE |
+------+-------+------+-------+
|     1 |    0 |     1 | 0 |
+------+-------+------+-------+
You can insert a Boolean value as follows: INSERT INTO [xxxx (XX)] VALUES (true), and of course values (1);
Examples are as follows:
Mysql> ALTER TABLE Test add IsOk boolean;
Query OK
mysql> desc test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| ID | Int (11) | NO | PRI | NULL | auto_increment |
| IsOk | tinyint (1) |     YES | |                NULL | |
+-------+-------------+------+-----+---------+----------------+
mysql> INSERT INTO Test (ISOK) values (true);
Query OK
Mysql> select IsOk from test;
+------+
| IsOk |
+------+
| 1 |
+------+
=================

MySQL does not have a Boolean type. This is also a very strange phenomenon. Example:
CREATE TABLE xs (ID int primary key, BL Boolean)
This is a good way to create a success, but looking at the statement after the build, you will find that MySQL replaced it with tinyint (1). In other words, MySQL has boolean=tinyint, but what type of Pojo class to define? because of inertial thinking, it is also defined as type in the Java class. Then use the <s:check/> tag in struts. This creates a serious problem. <s:check> is a Boolean, and Pojo is defined as byte. This data can never be submitted, was blocked by struts intercept. The workaround is to define a Boolean in the Pojo class, defined in MySQL as tinyint (1).  ------TINYINT (1) or ENUM (' true ', ' false ')-------

MySQL's Boolean and tinyint (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.