My friend Haibin asked me mysql While building the watch int What does the length after the type represent ? Is it the maximum width allowed to store values in this column ? Why do I set it to int(1), It's the same 10,100,1000 Well .

Although I knew at that time int(1), This length 1 It doesn't mean the width allowed to store , But there is no reasonable explanation . In other words, there is no real research on what this length represents , I usually use int(11), I don't know why 11 position . So I looked up some information on the Internet , I also looked at it carefully mysql The manual is about int data type That's what I'm saying .
 
Here's the storage and range of each integer type ( come from mysql manual )
 
type
byte
minimum value
Maximum
 
 
( Signed / Unsigned )
( Signed / Unsigned )
TINYINT
1
-128
127
 
 
0
255
SMALLINT
2
-32768
32767
 
 
0
65535
MEDIUMINT
3
-8388608
8388607
 
 
0
16777215
INT
4
-2147483648
2147483647
 
 
0
4294967295
BIGINT
8
-9223372036854775808
9223372036854775807
 
 
0
18446744073709551615
There are four columns in the table : Field type , Bytes occupied , The minimum value allowed to be stored , The maximum value allowed to store .
We take int Type as an example :
int type , The number of bytes occupied is 4byte, Students who have studied computer principles should know , byte (byte) It's not the smallest unit of computer storage , There are also bytes (byte) Smaller units , It's a bit (bit), One bit stands for one 0 or 1; 8 Bits make up a byte ; General bytes are in uppercase B To express byte, Place in lowercase b To express bit.
Conversion of computer storage units :
1B=8b
1KB=1024B
1MB=1024KB
 
So according to int The number of bytes allowed to be stored by type is 4 Bytes , We can work out int UNSIGNED( Unsigned ) The minimum value that can be stored for a type is 0, The maximum value is 4294967295( namely 4B=32b, The maximum value is 32 individual 1 form );
 
Next, let's talk about the field length when we create a table .
CREATE TABLE `test` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`number` INT( 5 ) NOT NULL
) ENGINE = MYISAM ;
With test Tabular number Field as an example , You see what I built is int(5)


 

mysql The length in the manual / Value to use "M" To represent the . Careful friends should have noticed mysql There is a saying in the manual :  M Indicates the maximum display width . The maximum effective display width is 255. The display width is independent of the range of values contained in the storage size or type ;
This sentence doesn't seem easy to understand , Because there's a keyword here that's easy to confuse , " Maximum display width " Our first reaction is that the maximum value of this field can allow the width of the stored value . Think we built int(1), You can't store data 10 了 , It doesn't mean that .
This M=5 We can simply understand it as , We built this length to tell MYSQL database The width of the data stored in this field is 5 digit ,  Of course, if you're not 5 digit ( As long as it is within the storage range of this type )MYSQL It can also be stored normally ,  This can explain the red mark above .
Let's put this field's " attribute " It is amended as follows UNSIGNED ZEROFILL Take a look at the effect .

 
We see now my number Field , length (M)=5, attribute =UNSIGNED ZEROFILL( Unsigned , use 0 To fill in the number of digits ),  After setting this property, I insert data into the table , The system will automatically turn number Field M Not enough 5 Bit on the left 0 Fill in ; The effect is as follows


 

There is another sentence in the manual " When mysql For some complex connection (join) When generating a temporary table , You may have problems , Because in this case ,mysql Trust that all values fit the original column width ". This also makes me wonder how to set the width properly ?
But one thing you should know clearly after reading this document , length M It has nothing to do with the size of the number you store .

Detailed explanation mysql int More related articles on the length value of type

  1. MySQL data type int(M) What do you mean ? Detailed explanation mysql int The length value problem of type

    MySQL In data type integer types It's a little strange. . You may see things like :int(3).int(4).int(8) And so on. int data type . Rigid contact MySQL When , I thought int(3) ...

  2. Detailed explanation mysql int The length value problem of type 【 turn 】

    mysql While building the watch int What does the length after the type represent ? Is it the maximum width allowed to store values in this column ? Why do I set it to int(1), It's the same 10,100,1000 Well . Although I knew at that time int(1), This length 1 It doesn't mean ...

  3. Detailed explanation MySQL int The length value problem of type

    Here's the storage and range of each integer type ( come from mysql manual )

  4. mysql int Length value of type

    Storage and range of integer types ( come from mysql manual ) type byte minimum value Maximum     ( Signed / Unsigned ) ( Signed / Unsigned ) TINYINT 1 -128 127     0 255 SMALLIN ...

  5. mysqlint Length value of type mysql While building the watch int What does the length after the type represent

    Detailed explanation mysql int Length value of type mysql While building the watch int What does the length after the type represent Is it the maximum width allowed to store values in this column Why do I set it to int(1), It's the same 10,100,1000 Well . Although I knew at that time i ...

  6. MySQL Stored procedure details mysql stored procedure

    Original address :MySQL Stored procedure details   mysql  Stored procedure author : Good evening for the king mysql Stored procedure details 1.      Introduction to stored procedures Our common operational database language SQL Statements need to be compiled before they are executed , And then execute , And storage ...

  7. [ Learn more Web Security ](5) Detailed explanation MySQL injection

    [ Learn more Web Security ](5) Detailed explanation MySQL injection 0x00 Catalog 0x00 Catalog 0x01 MySQL A brief introduction to injection 0x02 about information_schema The research of Library 0x03 The first step of the injection —— ...

  8. Mysql Commonly used show command ,show variables like xxx Detailed explanation ,mysql Runtime parameters

    MySQL There are a lot of basic commands ,show Orders are one of them , Among many users, there is no doubt that show The use of commands is also prone to confusion , This paper is a collection of show Many uses of commands . detailed : http://dev.mysql.com/doc/ ...

  9. 【 Article reading 】 Detailed explanation MySQL data type

    Detailed explanation MySQL data type - Cangjie in May - Blog Garden http://www.cnblogs.com/xrq730/p/8446246.html notes : about MySQL The data types of are explained in detail , This is the most ...

Random recommendation

  1. CRM Migration server notes

    1. install IIS 2. install sqlexpress 2005, sql management studio express 3. install .net framework4.0 4. install filezilla S ...

  2. HTML5 brief introduction 、 video

    HTML5 Some rules established : New features should be based on HTML.CSS.DOM as well as JavaScript. Reduce the need for external plug-ins ( such as Flash) Better error handling More tags to replace scripts HTML5 We should be alone ...

  3. stay C Language to get the function name programmatically

    Just to get the function name , Just embed a hard coded string in the body of the function , This method is tedious and easy to make mistakes , Let's see how to use the new C99 characteristic , Get the function name when the program is running . Object reflection Library . Debugging tools and code analyzer , You often need to access it at run time ...

  4. nodeJS Module addressing rules

    Introduction In a tutorial by Ruan Yifeng , Place the app in   npm Module installation directory directory of the same level (https://github.com/ruanyf/webpack-demos) Next . But in the application directory file , The use of reference standard library ...

  5. transfer hive, Different clusters .

    step1: Set the default to export hive The database for defaultDatabase On any node in the original cluster , newly build “.hiverc” file , Add the following : vi ~/.hiverc use defaultDa ...

  6. Apache coredump Problem discovery and resolution record

    Catalog Apache coredump Problem discovery and resolution record background Find the problem solve the problem Method 1 Method 2 summary Linux Program compilation link dynamic library version problem ldd command Dynamic library compilation and soname on-line ...

  7. Nginx( One )------ Introduction and installation

    Speaking of Nginx , Maybe the first thing you think of is the function of load balancing and reverse proxy . you 're right , This is also currently used Nginx The two most frequent features , however Nginx It's not just these two functions , Its role is still very big , This series of blogs is slow ...

  8. rpc Framework implementations ( Continuous updating )

    The scale of web applications continues to grow , Conventional vertical application architectures can no longer cope , Distributed service architecture and flow computing architecture are imperative ,rpc Long connection based remote procedure call applications . One :A The service call B service , The whole call process , The main steps are as follows :( Excerpt from ...

  9. jquery in ajax Of dataType All kinds of attribute meanings of

    Reference resources ajax api file :http://www.w3school.com.cn/jquery/ajax_ajax.asp dateType The type of parameter accepted after :string Expected data type returned by the server . ...

  10. SqlAlchemy operation ( One )

    The blog is reproduced in  http://www.cnblogs.com/haiyan123/p/8270520.html One . Introduce SQLAlchemy It's based on Python Realized ORM frame . The framework is based on DB ...