Skip to content

Instantly share code, notes, and snippets.

@akirattii
Last active July 24, 2022 08:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akirattii/864506c5a31ab3bf806e7c309eca0252 to your computer and use it in GitHub Desktop.
Save akirattii/864506c5a31ab3bf806e7c309eca0252 to your computer and use it in GitHub Desktop.
[MySQL] SQLのパフォーマンステスト用に郵便番号のデータベースでも作ってみる(郵便番号CSVのダウンロードからインポートまで)

郵便局のHPからken_all.zipをダウンロード(Download zipcode CSV file "ken_all.zip"):

$ wget http://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip

ZIPを解凍(Extract KEN_ALL.CSV from ken_all.zip):

$ unzip ken_all.zip

Shift_JISな文字コードを UTF-8 に変換(Convert original charset Shift_JIS to UTF-8):

$ nkf -w --overwrite KEN_ALL.CSV 

変換できたかチェック(Check charset):

$ nkf -g KEN_ALL.CSV
UTF-8 (CR)

MySQLから触れるディレクトリにCSVをコピー(Copy the CSV file to the directory governed by mysql server):

$ cp ./KEN_ALL.CSV /var/lib/mysql-files/

MySQL Clientを起動(Open mysql client):

$ mysql -h container-mysql -uroot -p

japanpost という DB を作って接続(Creates new database named "japanpost" then connect that):

mysql> CREATE DATABASE japanpost CHARACTER SET utf8; 
mysql> use japanpost;

zipcode というテーブルを作る(Creates new table named "zipcode"):

mysql> CREATE TABLE zipcode (
publicCode char(5) NOT NULL,
oldCode char(5) NOT NULL,
code char(7) NOT NULL,
prefectureKana varchar(10),
cityKana varchar(100),
townKana varchar(100),
prefecture varchar(10),
city varchar(200),
town varchar(200),
flgMultiCode tinyint(1),
flgKoazaBanchi tinyint(1),
flgChome tinyint(1),
flgMultiTown tinyint(1),
updateState tinyint(1),
updateReason tinyint(1),
PRIMARY KEY (code)
);

CSVファイルからインポート(Import zipcode list from the csv file):

mysql> LOAD DATA LOCAL INFILE '/var/lib/mysql-files/KEN_ALL.CSV' IGNORE INTO TABLE zipcode FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"';

取り込めたかチェック(Check if import is succeeded):

mysql> select count(code) from zipcode;
+-------------+
| count(code) |
+-------------+
|      119958 |
+-------------+
1 row in set (0.02 sec)

以上 That's all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment