You may already know that the length 1 of int(1) does not represent the allowed storage width! But many people haven't really studied what this length means. Today I will give a brief analysis! Let's take a look at a simple example of table creation: create table test( id int(11) unsigned NOT NULL AUTO_INCREMENT, uid int(3) NOT NULL, PRIMARY KEY (id) )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; Here we take the uid field as an example, we set it to int(3) So the question is, if we set int(3), can we not store the data 1234? Then you can test it by entering the following SQL statement insert into `test` (`uid`) VALUES(1234); insert into `test` (`uid`) VALUES(12345678); The resulting graph is as follows: Friends can find that the data 1234 is successfully inserted through the above SQL statement, and we can also insert more digits of data! Why is this? See below Here are the reasons: We can simply understand this int(n) as: This length is to tell MySQL that the width of the data stored in this field is n digits. Of course, if you do not store n digits, but (as long as it is within the storage range of this type) MySQL can also store it normally! Then we can create the test2 table again, and this time we add the following constraints to the uid field: unsigned and zerofill ==Field constraints will be discussed in detail later== The MySQL code is as follows: create table test2( id int(11) unsigned NOT NULL AUTO_INCREMENT, uid int(3) unsigned zerofill NOT NULL, PRIMARY KEY (id) )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; Now my uid field: length (n) = 3, field constraint = unsigned and zerofill: (unsigned constraint and use 0 to fill the digit constraint) After setting this constraint field, when inserting data into the table, the system will automatically fill the uid field with 0 on the left if it is less than 3 digits. You can test the code by inserting a number 33 into the uid field. insert into `test2` (`uid`) VALUES(33); The resulting graph is as follows: Are you surprised to find that when the length is less than 3, it is really filled with 0s from the left! Hahahaha So: Now we should know clearly that the length n after int has nothing to do with the size of the numeric value you store! ==Summary:== When defining the table field data type as int, the length n behind it is meaningless. As long as it is within the storage range of this type, MySQL can store it normally! If you must pad 0 on the left, then this field must have a zerofill constraint and an unsigned constraint! This is the end of this article about what the n after int(n) in MySQL means. For more relevant MySQL int(n) content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: JavaScript implements password box input verification
>>: Detailed explanation of the solution to docker-compose being too slow
The specific code for using jQuery to implement t...
First of all, what is a font icon? On the surface...
Ubuntu 20.04 has been released, bringing many new...
This article example shares the specific code of ...
Official documentation: So mysql should be starte...
Everyone has played the pinball and brick-breakin...
Table of contents 1. What does shallow copy mean?...
Introduction The Docker-Compose project is an off...
Preface I once encountered a difficult problem. I...
This article describes how to configure time sync...
Technical Background This application uses the vu...
Table of contents 1. Introduction to basic concep...
Recently, I plan to deploy a cloud disk on my hom...
Preface As a DBA, you will often encounter some M...
In daily work, we often need to view logs. For ex...