MySQL: Why specify display width without using zerofill

后端 未结 1 581
半阙折子戏
半阙折子戏 2021-01-02 17:43

I have recently delved into the exciting world of SQL. I am still trying to wrap my head around the concepts. I have been following tutorials online. Many of these tutorials

相关标签:
1条回答
  • 2021-01-02 18:11

    This additional "feature" display width is quite confusing, because in other column types like CHAR is specifies the length.

    Here is a short breakdown:

    • Most important: It is not specifying the "storage space" or "number of digits". It is just saying how the data in this column is formatted before it is returned. INT(5) can store the same values like INT(16) or INT(255) - all three can store all (and only) values that are valid for INT. INT(255) can not store a number with 255 digits. The storage space for all of them is the space an INT occupies.
    • If you use ZEROFILL on a column with display width, and the string representation of the stored number is shorter than the display width, it will be left-padded with zeros. If it is longer, nothing happens. If you choose INT(5) and the value is 13, it will be returned as 00013. If the value is 123456 it will be returned as 123456.
    • If you do not use ZEROFILL, there will be no padding at all (no spaces and such)
    • If you use ZEROFILL, you must be aware that the column also will be UNSIGNED
    • In any case, the display width is returned when the table meta data is queried. So an application could know how the data should be formatted.

    I dislike the display width, because the storage layer is "knowing" about visual presentation of the stored data. Besides this, there is no use that I am aware of.

    0 讨论(0)
提交回复
热议问题