What exactly is the number after the data type in mysql?

When mysql creates a new data table, we often see or add data after the data type, so what is the number after the data type? I used to think that int(3) means that the longest data is 3 bytes, but it is not! !

I insert into the num field:

INSERT INTO test (num) VALUES (123); success data 123,

INSERT INTO test (num) VALUES (123456); success data 123456, //then does the 3 in int(3) have no effect? The answer is right, it has no effect, where is his role?

explain:

The int type is fixed-length, and its capacity will not change with the following numbers, such as int(11) and int(8), which are the same and occupy 4 bytes. tinyint(1) and tinyint(10) also occupy one byte. So what does the 3 in the back stand for?

In the mysql database, data is constrained by data type (m), where the number m has different meanings in different data types. We only talk about integers here.

The integer number system has limited the range of values, tinyint occupies 1 byte, and int occupies 4 bytes. So the m behind the integer is not the length of the data, but the minimum length of the data displayed when it is displayed.

tinyint(1) The 1 here means the shortest display of one character. tinyint(2) The 2 here represents the shortest display of two characters.

When the character length exceeds (m), it is equivalent to nothing happening;

When the character length is less than (m), you need to specify a character to fill, such as zerofill (meaning to fill with 0),

Set tinyint(2) zerofill when you insert 1 it will display 01; set tinyint(4) zerofill when you insert 1 it will display 0001.

So, without zerofill, (m) is useless.

 

1 in int(1) and tinyint(1) in mysql only specifies the display length, not the storage length. It is only useful when the field specifies zerofill.

 

The bit limit is basically meaningless.

 

float(M,D)

The first number M represents the total length limit (the total length includes decimal places and integer digits), 2 means that the total length cannot exceed 2 characters, such as 2.34, which exceeds the total length of 2 digits;

The second number, D, represents the length limit of decimal places. 0.2 means 1 decimal place is used.

In this way, of course, M must be greater than or equal to D.

For example, if it is set to float(2,2), then when the number 12.3 is written, 0.99 is actually inserted.

For example, if it is set to float(2,1), then when the number 12.3 is written, 9.9 is actually inserted.

MySQL will automatically intercept the maximum value that the field can accept and store it.

Then set to float(0,0), it is equivalent to unlimited, or limited by the precision of float itself.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939369&siteId=291194637