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

前端 未结 4 1105
北荒
北荒 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:40

    There are a few problems with storing a zip code as a numeric value.

    1. Zip Codes have extensions, meaning they can be 12345-6789. You cannot store a dash in a numeric datatype.
    2. There are many zip codes that start with a zero, if you store it as an int you are going to lose the leading zero.
    3. You do not add/subtract, etc zip codes or use numeric functions with them.

    I would store a zip code as a varchar(5) or varchar(10).

    As a side note, I am not sure why you would select varchar(6), do you have a reason for selecting an unusual length when standard zip codes are 5 or 10 with the extension?

提交回复
热议问题