SoFunction
Updated on 2024-11-12

Explaining the Meaning of M in MySQL Datatype int(M)

present (sb for a job etc)

The integer types in MySQL datatypes are a bit strange. You may see int types such as int(3), int(4), int(8), etc. When I first got into MySQL, I thought that int(3) took up less storage space than int(4), and int(4) took up less storage space than int(3). When I was new to MySQL, I thought int(3) took up less storage than int(4), and int(4) took up less storage than int(8).

Later, referring to the MySQL manual, I realized that I had misunderstood.

int(M): M indicates the maximum display width for integer types.

In the integer data type, M represents the maximum display width.

It turns out that in int(M), the value of M has nothing to do with how much storage space int(M) takes up. int(3), int(4), and int(8) all take up 4 btyes of storage space on disk. To be clear, int(M) is the same as the int data type except for the way it is displayed to the user.

Also, int(M) only combines with zerofill so that we can clearly see the difference.

mysql> drop table if exists t;
mysql> create table t(id int zerofill);
mysql> insert into t(id) values(10);
mysql> select * from t;
+------------+
| id   |
+------------+
| 0000000010 |
+------------+
mysql> alter table t change column id id int(3) zerofill;
mysql> select * from t;
+------+
| id |
+------+
| 010 |
+------+
mysql>
mysql> alter table t change column id id int(4) zerofill;
mysql> select * from t;
+------+
| id |
+------+
| 0010 |
+------+
mysql>
mysql> insert into t(id) values(1000000);
mysql> select * from t;
+---------+
| id  |
+---------+
| 0010 |
| 1000000 |
+---------+

As can be seen from the above test, "(M)" specifies the width of the display of int-type values, if the field data type is int (4), then: when the display value of 10, the left side to be complementary "00"; when the display value of 100 is, the left side to be complementary "0"; when the display value of 1000000, has exceeded the specified width "(4)", so the output as is. When displaying the value of 1000000, it has exceeded the specified width "(4)", so it is output as it is.

When working with the integer types of the MySQL datatypes (tinyint, smallint, mediumint, int/integer, bigint), I can't think of any point in putting "(M)" after the datatype for non-special needs.

Here are some additional data types

1. Integral

MySQLdata type hidden meaning(notated)
tinyint(m) 1bytes realm(-128~127)
smallint(m) 2bytes realm(-32768~32767)
mediumint(m) 3bytes realm(-8388608~8388607)
int(m) 4bytes realm(-2147483648~2147483647)
bigint(m) 8bytes realm(+-9.22*10(used form a nominal expression)18(raised the) nth power)

If unsigned is added to the value range, the maximum value is doubled, such as tinyint unsigned has a value range of (0~256).
The m in int(m) is the width of the display in the result set of the SELECT query, and does not affect the actual range of values, it does not affect the width of the display, so I don't know what the use of this m is.

2, floating-point type (float and double)

MySQLdata type hidden meaning
float(m,d) Single precision floating point 8bit accuracy(4nibbles)  mtotal number,ddecimal fraction
double(m,d) double precision floating point type 16bit accuracy(8nibbles) mtotal number,ddecimal fraction

Set a field defined as float(5,3), if you insert a number 123.45678, the actual database is stored in 123.457, but the total number is also based on the actual, that is, 6 bits.

3. Fixed Points

Floating-point types store approximate values in the database, while fixed-point types store exact values in the database.

decimal(m,d) parametersm<65 is the total number of,d<30both (... and...) d<m is a decimal place。

4、String(char,varchar,_text)

MySQLdata type hidden meaning
char(n)  fixed length,at most255characters
varchar(n) variable length,at most65535characters
tinytext variable length,at most255characters
text  variable length,at most65535characters
mediumtext variable length,at most2(used form a nominal expression)24(raised the) nth power-1characters
longtext variable length,at most2(used form a nominal expression)32(raised the) nth power-1characters

5. Date and time data types

MySQLdata type hidden meaning
date  3nibbles,dates,specification:2014-09-18
time  3nibbles,timing,specification:08:42:30
datetime 8nibbles,datestiming,specification:2014-09-18 08:42:30
timestamp 4nibbles,自动存储记录修改的timing
year  1nibbles,particular year

summarize

The above is the entire content of this article, I hope the content of this article on your learning or work can bring some help, if there are questions you can leave a message to exchange.