MySQL display width and field length

Copyright statement: This article is an original article by the blogger and follows the CC 4.0 BY-NC-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Link to this article: https://blog.csdn.net/zgdwxp/article/details/102503090

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:intIt 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 4very 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) ZEROFILLproperties, the default space padding will be replaced with zeros; for example, columns INT(4) ZEROFILL, the value 5will be displayed as0005
      • If you specify a ZEROFILL numeric column, MySQL will automatically add the UNSIGNED attribute
    • 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 INTmaximum value is only 10 digits in decimal, so INT(11)the complete data can be displayed. But I personally don't think this is necessary.

Attached is a screenshot of the experiment, showing the role and effect of width at a glance:
insert image description here

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.