Data-wise, tinyint(1)
, tinyint(2)
, tinyint(3)
etc. are all exactly the same. They are all in the range -128 to 127 for SIGNED
or 0-255 for UNSIGNED
. As other answers noted the number in parenthesis is merely a display width hint.
You might want to note, though, that application=wise things may look different. Here, tinyint(1)
can take a special meaning. For example, the Connector/J (Java connector) treats tinyint(1)
as a boolean value, and instead of returning a numerical result to the application, it converts values to true
and false
. this can be changed via the tinyInt1isBit=false
connection parameter.
A tinyint(1) can hold numbers in the range -128 to 127, due to the datatype being 8 bits (1 byte) - obviously an unsigned tinyint can hold values 0-255.
It will silently truncate out of range values:
mysql> create table a
-> (
-> ttt tinyint(1)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> insert into a values ( 127 );
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values ( -128 );
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values ( 128 );
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into a values ( -129 );
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from a;
+------+
| ttt |
+------+
| 127 |
| -128 |
| 127 |
| -128 |
+------+
4 rows in set (0.00 sec)
mysql>
... unless you change the sql_mode
or change the server config:
mysql> set sql_mode=STRICT_ALL_TABLES;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into a values ( -129 );
ERROR 1264 (22003): Out of range value for column 'ttt' at row 1
mysql>
The value used in the DDL for the datatype (eg: tinyint(1)) is, as you suspected, the display width. However, it is optional and clients don't have to use it. The standard MySQL client doesn't use it, for example.
https://dev.mysql.com/doc/refman/5.1/en/integer-types.html
https://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
MySql: Tinyint (2) vs tinyint(1) - what is the difference?