What is the difference between MySQL TINYINT(2) vs TINYINT(1)?

MySQLMySQLi Database

The number 2 and 1 in TINYINT(2) vs TINYINT(1) indicate the display width. There is no difference between tinyint(1) and tinyint(2) except the width.

If you use tinyint(2) or even tinyint(1), the difference is the same. You can understand the above concept using zerofill option.

  • tinyint(1) zerofill
  • tinyint(2) zerofill

Let us create a table. The query to create a table is as follows −

mysql> create table tinyIntDemo
   -> (
   -> Number1 tinyint(1) zerofill,
   -> Number2 tinyint(2) zerofill
   -> );
Query OK, 0 rows affected (0.62 sec)

Insert record in the table using insert command. The query is as follows −

mysql> insert into tinyIntDemo values(1,1);
Query OK, 1 row affected (0.12 sec)

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

mysql> select *from tinyIntDemo;

The following is the output. Spot the difference between both in the below result −

+---------+---------+
| Number1 | Number2 |
+---------+---------+
|       1 |      01 |
+---------+---------+
1 row in set (0.00 sec)
raja
Published on 10-Jan-2019 11:53:40
Advertisements