The meaning of the M value in the int(M) and tinyint(M) numeric types in MySQL

When first contacting the MySQL database, the understanding of the M value behind the numeric types of int(M) and tinyint(M) is that the maximum value that can be inserted into the database cannot be greater than M;

Later, after working, I also used it while learning. The later understanding is that M means that the character length of the value inserted into the database cannot be greater than M. For example, int(4), the character length of 1234 and 1234 to be inserted is 4 , it can just be inserted into the database, but 12341 will not work, because it is 5 characters long, this is also hearsay, and I have never verified it;

Today, because I am often asked about database knowledge in interviews, I also want to understand the meaning of this M in depth today (both of the above understandings are wrong).

First, we create a data table test:

mysql> CREATE TABLE test(
    -> id1 int(1),
    -> id2 tinyint(1)
    ->);

We define id1 as int, and set the character length to 1, id2 is defined as tinyint, and also set the character length to 1;

Then insert the values ​​127, 127 respectively, and it turns out that both are inserted into the data table:

mysql > INSERT INTO test(id1,id2) values(127,127); // Run successfully

The result is that the insertion is successful. From this test, we can already know that my previous ideas are wrong. Next, we will do another experiment and insert data 128,128, that is, id=128, id2=128:

mysql> INSERT INTO test(id1,id2) values(128,128);//运行失败:ERROR 1264 (22003):Out of range value for column 'id2' at row 1

An error occurred, the id1 of the int type was inserted successfully, and the id2 of the tinyint type prompted out of range, why is this?

First of all, we must first understand a basic knowledge point, which is the following table: (from the W3C tutorial)

The numeric types in the above table are all fixed-length, that is to say, no matter how many values ​​you store, how large or small, the size of the bytes occupied is fixed. For example, for the int(1) set before, although the value of M is 1 character, the size of the space it occupies is always 4 bytes. In other words, you can store signed integers from - 2 147 483 648 to 2 147 483 647 include either number in between. int(1) and int(11) occupy 4 bytes, which can be stored in the above numbers, tinyint(1) and tinyint(4) occupy 1 byte, and can be stored in from -128 to 127 number, which is why in the previous test, int(1) inserted 128 successfully, while tinyint(1) inserted 128 but prompted that the length was exceeded.

So, what does this M value mean?

At this point, we can already find that even if the M value is set to 1, it can also store a value whose character length is greater than 1. So what if the stored character length is less than 1? Let's try it out:

First change the type of id1 to int(2), and then insert the data id1=1:

mysql > ALTER TABLE test Modify id1 int(2 );
 mysql > INSERT INTO test(id1) values(1);//The operation is successful, indicating that the value 1 has been inserted into the test table

Let's query the data in the table to see what the results are:

复制代码
mysql> SELECT * FROM test;
+------+
|  id1 |
+------+
|   1  |
+------+
复制代码

接下来,我们再修改一下id1的填充数据类型zerofill(表示用0填充),这里先知道如何操作即可,我们再从结果得出结论:

复制代码
mysql> ALTER TABLE test MODIFY id1 int(2) zerofill;
mysql> SELECT * FROM test;
+------+
|  id1 |
+------+
|  01  |
+------+
复制代码

现在是不是有些清楚了。我们设置的M值是2,没有设置zerofill用0填充时,对于操作没有任何影响,而设置了zerofill后,我们可以清楚地看到值1字符数不足M值,左前位置补0。我们也可以将M值设置成别的大小进行多次测试,这里就不进行测试了。

需要强调的是,不同的数据类型中的M值意义是不一样的,我们这里仅讨论整型中的M值。

从上面我们可以得到如下的结论:

1、整数型的数值类型已经限制了取值范围,有符号整型和无符号整型都有,而M值并不代表可以存储的数值字符长度,它代表的是数据在显示时显示的最小长度;

2、当存储的字符长度超过M值时,没有任何的影响,只要不超过数值类型限制的范围;

3、当存储的字符长度小于M值时,只有在设置了zerofill用0来填充,才能够看到效果,换句话就是说,没有zerofill,M值就是无用的。

 

总结:int(11),tinyint(1),bigint(20),后面的数字,不代表占用空间容量。而代表最小显示位数。这个东西基本没有意义,除非你对字段指定zerofill。

所以我们在设计mysql数据库时,建表时,mysql会自动分配长度:int(11)、tinyint(4)、smallint(6)、mediumint(9)、bigint(20)。

所以,就用这些默认的显示长度就可以了。不用再去自己填长度,比如搞个int(10)、tinyint(1)之类的,基本没用。而且导致表的字段类型多样化。

 

参考博客:http://www.cnblogs.com/stringzero/p/5707467.html



转载:https://www.cnblogs.com/totian/p/7065123.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324859205&siteId=291194637
^M