Test Summary of data types BOOL/BOOLEAN and TINYINT in MySQL database

Source: Internet
Author: User

Guide]

On the Internet, many people ask if mysql provides a boolean type? MySQL database does provide a BOOLEAN type. Keywords of this data type can be written as boolean or boolean. But is the BOOLEAN Type of MySQL database data the same as that of other database products? This article explores the true face of the mysql boolean type.

(1) Data Type Test

(1). BOOLEAN type BOOL/BOOLEAN and tiny integer TINYINT

A) create a test table structure

 
 
  1. root@localhost : test 05:12:49> CREATE TABLE boolean_test(ID INT NOT NULL AUTO_INCREMENT,  
  2.    
  3.     ->                           Online_Flag BOOL,  
  4.    
  5.     ->                           Lock_Flag BOOLEAN,  
  6.    
  7.     ->                           PRIMARY KEY(ID)  
  8.    
  9.     ->                           )ENGINE=InnoDB CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';  
  10.    
  11. Query OK, 0 rows affected (0.01 sec) 

We can find that the SQL Syntax of MySQL allows the field type to be written as BOOL or BOOLEAN. In addition, we can use the SHOW command to check the created table structure:

 
 
  1. *************************** 1. row ***************************  
  2.    
  3.        Table: boolean_test  
  4.    
  5. Create Table: CREATE TABLE `boolean_test` (  
  6.    
  7.   `ID` int(11) NOT NULL AUTO_INCREMENT,  
  8.    
  9.   `Online_Flag` tinyint(1) DEFAULT NULL,  
  10.    
  11.   `Lock_Flag` tinyint(1) DEFAULT NULL,  
  12.    
  13.   PRIMARY KEY (`ID`)  
  14.    
  15. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8  
  16.    
  17. 1 row in set (0.00 sec) 

Summary:

We can see the difference between the structure definition of the boolean_test table created by manual input and the table structure definition in the database:

  • The MySQL database converts the Data Type BOOL/BOOLEAN of the field to TINYINT (1) by default );
  • No error or warning message is prompted during the data type conversion process automatically completed by the MySQL database;

B) Write Test Data

 
 
  1. root@localhost : test 05:12:58> INSERT INTO boolean_test(Online_Flag,Lock_Flag) VALUES(TRUE,FALSE);  
  2.    
  3. Query OK, 1 row affected (0.00 sec)  
  4.    
  5. root@localhost : test 05:13:58> INSERT INTO boolean_test(Online_Flag,Lock_Flag) VALUES(1,0);  
  6.    
  7. Query OK, 1 row affected (0.00 sec)  
  8.    
  9. root@localhost : test 05:14:04> INSERT INTO boolean_test(Online_Flag,Lock_Flag) VALUES(2,-1);  
  10.    
  11. Query OK, 1 row affected (0.00 sec)  
  12.    
  13. root@localhost : test 05:14:11> INSERT INTO boolean_test(Online_Flag,Lock_Flag) VALUES(-128,127);  
  14.    
  15. Query OK, 1 row affected (0.00 sec) 
 
 
  1. root@localhost : test 05:14:18> INSERT INTO boolean_test(Online_Flag,Lock_Flag) VALUES(-256,256);  
  2.    
  3. Query OK, 1 row affected, 2 warnings (0.00 sec)  
  4.    
  5. root@localhost : test 05:14:24> SHOW WARNINGS;  
  6.    
  7. +---------+------+------------------------------------------------------+  
  8.    
  9. | Level   | Code | Message                                              |  
  10.    
  11. +---------+------+------------------------------------------------------+  
  12.    
  13. | Warning | 1264 | Out of range value for column 'Online_Flag' at row 1 |  
  14.    
  15. | Warning | 1264 | Out of range value for column 'Lock_Flag' at row 1   |  
  16.    
  17. +---------+------+------------------------------------------------------+  
  18.    
  19. 2 rows in set (0.00 sec) 

Summary:

  • Two Boolean fields in boolean_test of the test data table. When the written value exceeds the storage range of the signed integer TINYINT data type, the warning message of field value truncation is displayed;
  • The boolean_test field in the test data table can be written to the TRUE or FALSE field that expresses the Boolean value. No error is reported and it does not need to be enclosed by single quotation marks or double quotation marks;
  • You can write a non-Boolean value to the boolean_test field of the test data table. MySQL does not receive any error or warning information;

C). display the data written into the boolean_test table.

 
 
  1. root@localhost : test 06:31:33> SELECT * FROM boolean_test;  
  2.    
  3. +----+-------------+-----------+  
  4.    
  5. | ID | Online_Flag | Lock_Flag |  
  6.    
  7. +----+-------------+-----------+  
  8.    
  9. |  1 |           1 |         0 |  
  10.    
  11. |  2 |           1 |         0 |  
  12.    
  13. |  3 |           2 |        -1 |  
  14.    
  15. |  4 |        -128 |       127 |  
  16.    
  17. |  5 |        -128 |       127 |  
  18.    
  19. +----+-------------+-----------+  
  20.    
  21. 5 rows in set (0.00 sec) 

Summary:

By checking the data in the boolean_test test table, we can find that the values stored in the MySQL database are different from the INSERT statements written into the data, as shown in:

  • The written Boolean value TRUE is converted to 1, and FALSE is converted to 0;
  • Values that exceed the upper and lower limits of TINYINT data type storage are automatically truncated;
  • Boolean bool/BOOLEAN functions are equivalent to tiny integer TINYTINT;

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.