0
0

More than 3 years have passed since last update.

🩰yawacom - 🐭どんな値がデータベースに入るのか

Last updated at Posted at 2021-01-27

2021/01/27現在編集中

どんな値がデータベースに入るのか

アプリ内でどんな値が必要なのか,その値はどんな状態であるべきなのかをmiroに書き出しました.
スクリーンショット 2021-01-27 14.57.27.png

テーブルは

  • ユーザ情報の格納:userテーブル
  • やわらかさの格納:yawarakasaテーブル
  • アクセストークンの格納:access tokenテーブル

の3つを用意します.
access tokenはログイン状態の保持のために作成しています.

準備

まずログインしてからデータベースを作ります.

$ mysql -uroot    # rootでログイン
mysql> create database yawarakasa;

これでyawarakasaという名前のデータベースが作られました.
確認してみましょう.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| yawarakasa         |
+--------------------+

どのデータベースを使うのかを指定します.

mysql> use yawarakasa

テーブル作成

テーブルをMySQLで作って行きます.
まずユーザテーブルです.

create_table.sql
use yawarakasa;

create table user(
  # auto_incrementは連番を自動で入れてくれるやつ
  # primary keyにはすでにユニーク制約が入っとる
  id int(255) not null primary key auto_increment,
  user_name varchar(255) not null,
  password varchar(255) not null,
  sex char(1) not null,
  unique(user_name)
);

次はやわらかさテーブルです.このファイルに追記で書いていきます.

create_table.sql
create table yawarakasa(
  # yawarakasaとしてのプライマリーキーもあるとべんり
  # primary keyは1行目に書かないとシンタックスエラー
  id int(255) not null primary key auto_increment,
  user_id int(255) not null references user(id),
  yawarakasa int(255) not null,
  year int(4) not null,
  month int(2) not null,
  day int(2) not null
);

最後にアクセストークンテーブルです.これもファイルに追記で買いていきます.

create_table.sql
create table access_token(
  user_id int(255) not null references user(id),
  access_token varchar(255) not null,
  # 古いアクセストークンでアクセスできなくするために最新トークン以外消すとかするといいかも?
  # デフォルトで現在時刻入れとく
  created_at datetime default current_timestamp not null
);

以上のSQLをファイル実行してテーブルを作成します.ファイル名はcreate_table.sqlにしました.
下記コマンドを行うことでファイルのSQL文を実行できます.

mysql> source create_table.sql
もしくは
mysql> \. create_table.sql

作られたかどうかを確認します.

mysql> show tables;
+----------------------+
| Tables_in_yawarakasa |
+----------------------+
| access_token         |
| user                 |
| yawarakasa           |
+----------------------+

テーブルが3つ作成されました!🎉

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