1
1

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 1 year has passed since last update.

MySQLで家計簿を作る

Last updated at Posted at 2022-12-11

お金の管理は大事だなと思い始めた今日この頃、
Excelもいいけど、練習も兼ねてSQLで管理しようと思い立ちました。

家計簿データベースの準備

家計簿データベースとテーブルを用意します。

ターミナル
-- mysqlに入る
mysql -u root -p

-- 家計簿データーベースを作成する
mysql> create database kakeibo;

-- 作成したデータベースを確認する
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kakeibo            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

-- kakeiboデータベースを選択する
mysql> use kakeibo;
Database changed

-- 費目管理用のテーブルを作成する
create table himoku (id int not null auto_increment primary key, name varchar(20) not null);

-- 確認する
mysql> show columns from himoku;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

-- 家計簿用テーブルを作成する
create table kakeibo (id int not null auto_increment primary key, item_id int not null, amout int, income int, create_date date not null, foreign key(item_id) references himoku(id));

-- 確認する
mysql> show columns from kakeibo;
+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| id          | int(11) | NO   | PRI | NULL    | auto_increment |
| item_id     | int(11) | NO   | MUL | NULL    |                |
| amout       | int(11) | YES  |     | NULL    |                |
| income      | int(11) | YES  |     | NULL    |                |
| create_date | date    | NO   |     | NULL    |                |
+-------------+---------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

データを追加して確認

ターミナル
-- Insert文でデータを追加
mysql> insert into himoku (name) values ('飲食費');

mysql> select * from himoku;
+----+----------------+
| id | name           |
+----+----------------+
|  1 | 飲食費          |
+----+----------------+
1 rows in set (0.00 sec)

-- kakeiboテーブルに詳細カラムを追加する
mysql> alter table kakeibo add detail varchar(100);

mysql> show columns from kakeibo;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| item_id     | int(11)      | NO   | MUL | NULL    |                |
| amout       | int(11)      | YES  |     | NULL    |                |
| income      | int(11)      | YES  |     | NULL    |                |
| create_date | date         | NO   |     | NULL    |                |
| detail      | varchar(100) | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

-- kakeiboテーブルにデータを追加する
mysql> insert into kakeibo (item_id, amout, create_date) values (1, 1605, '2022-12-11');
Query OK, 1 row affected (0.00 sec)

mysql> select * from kakeibo;
+----+---------+-------+--------+-------------+--------+
| id | item_id | amout | income | create_date | detail |
+----+---------+-------+--------+-------------+--------+
|  1 |       1 |  1605 |   NULL | 2022-12-11  | NULL   |
+----+---------+-------+--------+-------------+--------+
1 row in set (0.00 sec)

簡単な家計簿ができました。

参考資料

MySQLの使い方
MySQLでテーブルのカラムを表示して確認する
【SQL入門】外部キーとは?主キーとの関係や作成方法について解説

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?