ChatGPT解决这个技术问题 Extra ChatGPT

MySql: Tinyint (2) vs tinyint(1) - what is the difference?

I knew boolean in mysql as tinyint (1).

Today I see a table with defined an integer like tinyint(2), and also others like int(4), int(6) ...

What does the size means in field of type integer and tinyint ?

Read about MySQL Integer data types here.
@AamirR's answer is the correct one
@evilReiko zerofill also doesn’t make a difference in how the value is stored, it’s really about presentation ... spaces or zeroes don’t really make a difference in the correctness of anyone’s answer imo

J
Ja͢ck

The (m) indicates the column display width; applications such as the MySQL client make use of this when showing the query results.

For example:

| v   | a   |  b  |   c |
+-----+-----+-----+-----+
| 1   | 1   |  1  |   1 |
| 10  | 10  | 10  |  10 |
| 100 | 100 | 100 | 100 |

Here a, b and c are using TINYINT(1), TINYINT(2) and TINYINT(3) respectively. As you can see, it pads the values on the left side using the display width.

It's important to note that it does not affect the accepted range of values for that particular type, i.e. TINYINT(1) still accepts [-128 .. 127].


what does it mean 'column width' ??
@realtebo it's mainly used by the mysql command line client for display purposes.
The number of "digits" shown by the interface. In other words, it will pad zeros at the left. This kind of feature made some sense in the early days of databases, but now it is just legacy.
@Kailas If you're not using the mysql client? None at all.
@Kailas No! tinyint(1) doesn't accept only 0-9. It accepts the whole range of what tinyint can hold.
A
AamirR

It means display width

Whether you use tinyint(1) or tinyint(2), it does not make any difference.

I always use tinyint(1) and int(11), I used several mysql clients (navicat, sequel pro).

It does not mean anything AT ALL! I ran a test, all above clients or even the command-line client seems to ignore this.

But, display width is most important if you are using ZEROFILL option, for example your table has following 2 columns:

A tinyint(2) zerofill

B tinyint(4) zerofill

both columns has the value of 1, output for column A would be 01 and 0001 for B, as seen in screenshot below :)

https://i.stack.imgur.com/L68G2.png


Weirdly enought I just discovered that in the C# official MySQL connector tinyint(1) never returns values different from 0 and 1. This probably has to do with tinyint(1) automatically being recognized as a boolean. Just a heads up it sometimes matters.
Isn't it better to use a integer type and length as small as possible? I was under the impression this saved memory in MySQL.
As @Daniel-Sharp points out it could seem to matter to connectors. Just had an issue with Hibernate using JDBC reporting it interpreted a TINYINT(1) as a BIT.
@DanielSharp that is probably due to the connection option tinyInt1isBit which defaults to true. See dev.mysql.com/doc/connector-j/8.0/en/…
I always thought this had something to do with the amount of numbers being accepted (for example, int(4) not allowing anything above 9999). Guess I always thought wrong then. In both PHP and MySQL command line I see the value above it fine.
z
zloctb
mysql> CREATE TABLE tin3(id int PRIMARY KEY,val TINYINT(10) ZEROFILL);
Query OK, 0 rows affected (0.04 sec)

mysql> INSERT INTO tin3 VALUES(1,12),(2,7),(4,101);
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM tin3;
+----+------------+
| id | val        |
+----+------------+
|  1 | 0000000012 |
|  2 | 0000000007 |
|  4 | 0000000101 |
+----+------------+
3 rows in set (0.00 sec)

mysql>

mysql> SELECT LENGTH(val) FROM tin3 WHERE id=2;
+-------------+
| LENGTH(val) |
+-------------+
|          10 |
+-------------+
1 row in set (0.01 sec)


mysql> SELECT val+1 FROM tin3 WHERE id=2;
+-------+
| val+1 |
+-------+
|     8 |
+-------+
1 row in set (0.00 sec)

Please edit your answer to include some explanation. Code-only answers do very little to educate future SO readers. Your answer is in the moderation queue for being low-quality.
D
Devart

About the INT, TINYINT... These are different data types, INT is 4-byte number, TINYINT is 1-byte number. More information here - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT.

The syntax of TINYINT data type is TINYINT(M), where M indicates the maximum display width (used only if your MySQL client supports it).

Numeric Type Attributes.


what does it mean 'display width' !?
From the reference - 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.
But I have not seen applications or MySQL clients which use this feature. I always use INT(11) and use formatting functions to customize output.