1
5

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.

レシートをデータベースに保存して家計簿的な感じにしたい その2 テーブル作成

Posted at

こんばんは。
日付は5月8日になり、GWは終わりを迎えましたね。
今年は特に出かけたりできませんでした。
来年はどこか旅行に行きたいものです。

前回はデータベース設計?をしました。
今回は実際にテーブルを作ってみようと思います。
よろしくお願いします!

テーブルの詳細決め

前回で大枠は作りましたが、カラムの名前や型はまだ決めてなかったです。
まずはそこから決めていこうと思います。

receipt

カラム名 NULL許可 初期値 主キー
id uint だめ auto_increment そう
payment_date timestamp だめ 0 ちがう
store_id uint だめ 1 ちがう
payment_amount uint だめ ない ちがう
payment_type_id uint だめ 1 ちがう

store

カラム名 NULL許可 初期値 主キー
id uint だめ auto_increment そう
store_name_id uint だめ 1 ちがう
branch_store_name_id uint だめ 1 ちがう

store_name

カラム名 NULL許可 初期値 主キー
id uint だめ auto_increment そう
store_name varchar(255) だめ ない ちがう

branch_store_name

カラム名 NULL許可 初期値 主キー
id uint だめ auto_increment そう
branch_store_name varchar(255) だめ ない ちがう

item_quantity

カラム名 NULL許可 初期値 主キー
receipt_id uint だめ ない ちがう
item_id uint だめ ない ちがう
quantity uint だめ 1 ちがう

item_price

カラム名 NULL許可 初期値 主キー
receipt_id uint だめ ない ちがう
item_id uint だめ ない ちがう
price uint だめ ない ちがう

item

カラム名 NULL許可 初期値 主キー
id uint だめ auto_increment そう
item_name varchar(255) だめ ない ちがう

payment_type

カラム名 NULL許可 初期値 主キー
id uint だめ auto_increment そう
payment_type varchar(255) だめ ない ちがう

大体NULLはだめにしました。
ググりつつ自力にで考えたので、おかしな部分があるかもです。
(auto_incrementの始まりって1からだったんですね。最初に初期値を0にして失敗しました。)

テーブル作成

詳細が決まったので、実際に作ってみます。

create database household_accounts;
use household_accounts;

create table receipt (
  id int unsigned not null auto_increment primary key,
  payment_date timestamp not null default 0,
  store_id int unsigned not null default 1,
  payment_amount int unsigned not null,
  payment_type_id int unsigned not null default 1
);

create table store (
  id int unsigned not null auto_increment primary key,
  store_name_id int unsigned not null default 1,
  branch_store_name_id int unsigned not null default 1
);

create table store_name (
  id int unsigned not null auto_increment primary key,
  store_name varchar(255) not null
);

create table branch_store_name (
  id int unsigned not null auto_increment primary key,
  branch_store_name varchar(255) not null
);

create table item_quantity (
  receipt_id int unsigned not null,
  item_id int unsigned not null,
  quantity int unsigned not null default 1
);

create table item_price (
  receipt_id int unsigned not null,
  item_id int unsigned not null,
  price int unsigned not null
);

create table item (
  id int unsigned not null auto_increment primary key,
  item_name varchar(255) not null
);

create table payment_type (
  id int unsigned not null auto_increment primary key,
  payment_type varchar(255) not null
);

(裏で何回かミスを書き直したりしてるので、間違ってる部分があるかもです。)
これでからっぽのテーブル達ができたはずです。

汎用データ入れる

店名が店舗名がわからない時用のレコードを入れときます。

insert into store_name (id, store_name) values (1, '不明');
insert into branch_store_name (id, branch_store_name) values (1, '不明');
insert into store (id, store_name_id, branch_store_name_id )values (1, 1, 1);

これで、店名が不明の場合はreceiptのstore_idに1入れれば大丈夫ですね。

商品名もなかったり分からなかった場合用に不明を入れときます。

insert into item values (1, '不明');

支払い方法はとりあえず今思いつく、現金、クレジットカード、suica、iDを入れときます。

insert into payment_type (id, payment_type) values
  (1, '現金'),
  (2, 'クレジットカード'),
  (3, 'suica'),
  (4, 'iD');

今回はひとまずこれで終わりにします。

次回の予定

次は実際にレシートのデータを入れてみたいと思います。
が、生のMySQLでデータ入力は気が滅入りそうなので、何かしらの何かで簡単に入れられるようにしたいですね。
何かいい方法がないか探してみます。
それでは、失礼します。:walking_tone1:

1
5
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
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?