0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【MySQL】電話番号と郵便番号の頭の0が消える現象

0
Last updated at Posted at 2020-06-29

開発中に電話番号と郵便番号の頭の0が消える現象が起こりました。

これはデータベースの仕様ですが、WEBアプリを作成するにあたって、頭の0が必要なもの(電話番号や郵便番号等)を扱う機会は多いです。

電話番号と郵便番号の管理方法

郵便番号
日本国内だけであれば、「3桁 + 4桁]で固定の為、Int(7)で管理する。
ただし、頭の0が欠けちゃうので、ハイフンを取り除き、桁数を固定し、ZEROFILLオプションを付けておく。

※ZEROFILLとは、桁数が足りなかった時に、足りない分を0で埋めてくれるもの。

海外も対応する必要あれば、ルールが国によって異なります。
何かしら法則があって調整できればIntで良いかもしれないですが、対応する国が増えたり、色々ややこしそうであれば、Varcharで管理しておくのが良いかもしれません。

電話番号
桁数がまちまちなので、Varcharで管理するのが一般的なようです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?