このページでは,日本郵政公社「ゆうびんホームページ」で公開されている 2つの郵便番号データのCSV(カンマ区切り値)形式ファイルを使う.
これらのファイルを,リレーショナルデータベース管理システム SQLite のテーブル(テーブル名は zipall)に格納します.
謝辞:
・郵便番号データについて、日本郵政公社に感謝します
SQLite 3 のインストールと利用の入門: 別ページにまとめている.
このページでは,SQLite 3 のデータベースの作成を行う. 作成するデータベースのデータベース名を決めておくこと. このページでは,次のように書く.
データベース名は,自由に決めてよいが,半角文字(つまり英字と英記号)を使い,スペースを含まないこと,
※ ファイルは C:\data\ken_all.csv, C:\data\jigyosyo.CSV であるとして説明を続ける.
sqlite3
.open --new C:/sqlite3/zipdb
次の2つのテーブルを定義する.
◆ SQL プログラム
CREATE TABLE KEN_ALL (
a0 integer not null,
a1 integer not null,
a2 integer not null,
a3 text not null,
a4 text not null,
a5 text not null,
a6 text not null,
a7 text not null,
a8 text not null,
a9 boolean not null,
a10 boolean not null,
a11 boolean not null,
a12 boolean not null,
a13 boolean not null,
a14 integer not null
);
create table JIGYOSYO (
a0 integer not null,
a1 text not null,
a2 text not null,
a3 text not null,
a4 text not null,
a5 text,
a6 text,
a7 integer not null,
a8 integer not null,
a9 text not null,
a10 boolean not null,
a11 integer not null,
a12 boolean not null
);
次のコマンドを実行 (Windows 用)
.mode csv
.import C:\\data\\ken_all.csv KEN_ALL
.import C:\\data\\jigyosyo.CSV JIGYOSYO
vacuum;
次を実行し,先頭3行を確認してみる.
select * from KEN_ALL limit 3;
select * from JIGYOSYO limit 3;
JIGYOSYO テーブルでは,a0 の値が 1つに決まれば,a4 の値が 1つに決まる.
それを,次の SQL で確認する.結果として何も表示されなければ OK.
create table T as select distinct a0, a3, a4 from JIGYOSYO;
SELECT * FROM T WHERE a0 IN ( SELECT a0 FROM T group by a0 HAVING COUNT(*) > 1 );
KEN_ALL テーブルでは,a0 の値が 1つに決まれば,a7 の値が 1つに決まる.
それを,次の SQL で確認する.結果として何も表示されなければ OK.
drop table T;
create table T as select distinct a0, a6, a7 from KEN_ALL;
SELECT * FROM T WHERE a0 IN ( SELECT a0 FROM T group by a0 HAVING COUNT(*) > 1 );
次の SQL で確認する.結果として何も表示されなければ OK.
select * from JIGYOSYO where a1 = '""';
次の SQL で確認する.結果として何も表示されなければ OK.
select * from JIGYOSYO where a2 = '""';
select * from JIGYOSYO where a3 = '""';
select * from JIGYOSYO where a4 = '""';