Home  >  Article  >  Database  >  Does MySQL boolean "tinyint(1)" support up to 127?

Does MySQL boolean "tinyint(1)" support up to 127?

PHPz
PHPzforward
2023-09-19 16:29:04994browse

MySQL 布尔值“tinyint(1)”是否最多支持 127?

Let us understand some key points about TINYINT type in MySQL -

  • TINYINT type occupies 1 byte i.e. 8 bits.
  • TINYINT(N), where N represents the display width you want.

For example, TINYINT(1) can be used to display a width of 1.

Let us understand the minimum and maximum values ​​-

The maximum value for tinyint is= (2(8-1)-1) = 127
The minimum value for tinyint is = -(2(8-1)) = -128.

The value will be between -128 to 127. This means that TINYINT (1) does not affect the maximum and minimum values ​​of tinyint.

Let's check it out -

First, create a table with its columns set to TINYINT (1) -

mysql> create table Display
   -> (
   -> rangeOfId tinyint(1)
   -> );
Query OK, 0 rows affected (0.67 sec)

Let's insert a value that is outside the maximum and minimum range . This will result in the error -

mysql> insert into Display values(128);
ERROR 1264 (22003): Out of range value for column 'rangeOfId' at row 1

The query to insert records is as follows. We will now insert values ​​in the range -

mysql> insert into Display values(127);
Query OK, 1 row affected (0.18 sec)

mysql> insert into Display values(-128);
Query OK, 1 row affected (0.20 sec)

Use select statement to display all records in the table. The query is as follows -

mysql> select *from Display;

Output

+-----------+
| rangeOfId |
+-----------+
|       127 |
|      -128 |
+-----------+
2 rows in set (0.00 sec)

The above is the detailed content of Does MySQL boolean "tinyint(1)" support up to 127?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete