MySQL display width and field length
foreword
If you have seen int(4)
it and found that it can write 10000, 100000 or even larger data, then this article can help you clear your doubts.
Focus first:int
It is a fixed-length field, and the storage overhead is fixed at 4 bytes.
For the field length and storage overhead of all fields in MySQL, please refer to "Length Range and Storage Overhead of All Types of MySQL (Distinguish Display Width/Signed or Not)"
Since it is a fixed-length field, this is 4
very eye-catching. It is the display width to be said next: it only means that when the stored value is insufficient 4
, it is displayed as a width of 4 characters.
Specific analysis
The display width of MySQL only affects the display of numbers and has no effect on data storage.
- Display width is optional extension, can not be written
- Display width is calculated by number of characters
- If the stored data is not enough to display the width, fill it with spaces;
- Used with optional (non-standard)
ZEROFILL
properties, the default space padding will be replaced with zeros; for example, columnsINT(4) ZEROFILL
, the value5
will be displayed as0005
- If you specify a ZEROFILL numeric column, MySQL will automatically add the UNSIGNED attribute
- Used with optional (non-standard)
- The stored data exceeds the display width, and the complete data is displayed;
- The display width can exceed the storage length, and the blanks are filled according to the display width.
It is generally used to indicate that the complete data is displayed. For example, the
INT
maximum value is only 10 digits in decimal, soINT(11)
the complete data can be displayed. But I personally don't think this is necessary.
- If the stored data is not enough to display the width, fill it with spaces;
Attached is a screenshot of the experiment, showing the role and effect of width at a glance:
For the problem of display width, please refer to the official documentation: https://dev.mysql.com/doc/refman/5.6/en/numeric-type-attributes.html
above. Thanks for reading.