Does MySQL Boolean “tinyint(1)” holds values up to 127?

MySQLMySQLi Database

Let us learn some points about TINYINT type in MySQL −

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

For example, TINYINT(1) can be used to display width which is 1.

Let us learn about 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 TINYINT (1) does not affect the maximum and minimum value of tinyint.

Let us check it −

Firstly, create a table with a column set as TINYINT (1) −

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

Let us insert a value beyond the maximum and minimum range. This will result in an 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 the values within 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)

Display all records from the table using select statement. The query is as follows −

mysql> select *from Display;

Output

+-----------+
| rangeOfId |
+-----------+
|       127 |
|      -128 |
+-----------+
2 rows in set (0.00 sec)
raja
Published on 10-Jan-2019 14:53:16
Advertisements