What is the best column type for a United States ZIP code?

前端 未结 4 1116
北荒
北荒 2021-02-19 07:12

I want to store Zip Code (within United States) in MySQL database. Saving space is a priority. which is better option using VARCHAR - limited

4条回答
  •  遇见更好的自我
    2021-02-19 07:49

    Zip codes are always 5 characters, hence you would need a CHAR datatype, rather than VARCHAR.

    Your options are therefore

    CHAR(5)
    
    MEDIUMINT (5) UNSIGNED ZEROFILL
    

    The first takes 5 bytes per zip code.

    The second takes only 3 bytes per zip code. The ZEROFILL option is necessary for zip codes with leading zeros.

    So, if space is your priority, use the MEDIUMINT.

提交回复
热议问题