4

I am confused to see this tinyint(3) structure. If tinyint can store only 1 byte then what is the purpose of giving the size in brackets? Is there something special with this parameter in MySQL?

2 Answers 2

8

That's not bytes, it's display width. It's used as a hint for the maximum expected typical character width of values for the column, to applications that care to actually use the metadata -- which many don't.

As explained in the documentation:

MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)

The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits.

The default value for an unsigned tinyint is (3) because that's the size of "255" which the widest number (in displayed character positions) that can be stored in an an unsigned tinyint.

The display width also does not change the storage requirements for the data types; so, for example, an INT(1) still takes up 4 bytes of storage.

0
1

It's just used when zerofill is activated, see bellow:

mysql> DESCRIBE zero_fill;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| a     | tinyint(3)                   | YES  |     | NULL    |       |
| b     | tinyint(3) unsigned zerofill | YES  |     | NULL    |       |
+-------+------------------------------+------+-----+---------+-------+

we have 2 fields with same size, but b has zerofill activated, lets insert some rows in this table

mysql> INSERT INTO zero_fill SET a=1, b=1;
Query OK, 1 row affected, 2 warnings (0.06 sec)

mysql> INSERT INTO zero_fill SET a=11, b=11;
Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO zero_fill SET a=111, b=111;
Query OK, 1 row affected (0.03 sec)

and now when we retrieve this row we see the difference:

mysql> SELECT * FROM zero_fill;
+------+------+
| a    | b    |
+------+------+
|    1 |  001 |
|   11 |  011 |
|  111 |  111 |
+------+------+

on b column mysql add left zero's if the size is less the what is inside the brackets tinyint(3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.