3

I can not understand why this option. The signed TINYINT data type can store integer values between -128 and 127.

mysql> create table b (i tinyint(1));

mysql> insert into b values (42);

mysql> select * from b;
+------+
| i    |
+------+
|   42 |
+------+
Daniel O'Hara
  • 13,307
  • 3
  • 46
  • 68
zloctb
  • 10,592
  • 8
  • 70
  • 89
  • possible duplicate of [MySql: Tinyint (2) vs tinyint(1) - Which difference?](http://stackoverflow.com/questions/12839927/mysql-tinyint-2-vs-tinyint1-which-difference) – jmail Apr 16 '14 at 14:30

2 Answers2

4

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?

Community
  • 1
  • 1
jmail
  • 5,944
  • 3
  • 21
  • 35
1

The lenght parameter for numeric data types only affect the display width, but not the value that can be stored.

TINYINT   -128 to 127 (or 0-255 unsigned)
SMALLINT  -32768 to 32767 (or 0-65535 unsigned)
MEDIUMINT -8388608 to 8388607 (or 0-16777215 unsigned)
INT       -2147483648 to 2147483647 (or 0-4294967295 unsigned)
BIGINT    -9223372036854775808 to 9223372036854775807 (or 0-18446744073709551615 unsigned)
ek9
  • 3,392
  • 5
  • 23
  • 34