Powered by NotePM

ER図の書き方

Published on 31/05/2023 04:16
  • 3264

ER図とは

ER図とは、簡単にいうとDBの設計図です。テーブルやカラム、テーブル同士のリレーションを1つの図面で表します。リレーションについて復習したい方は、カリキュラム9-2を参照してください。
また、一般的に、ER図には幾つか種類がありますが、今回は簡易的に作成します。

命名規則について

【テーブル設計をする際の命名規則】

  • 小文字の複数形
  • 複数単語を繋げる場合はスネーク記法(単語と単語の間をアンダーバーで繋げる)
  • 中間テーブルの場合は、テーブル名の単数系_テーブル名の単数系(アルファベット順)
    ex.)postsテーブルとusersテーブルの中間テーブルの場合、post_userテーブルとなる

主キーと外部キーの記載方法〜PKとFK〜

ER 図において、

  • PK=主キー
  • FK =外部キー

の意味で用いられます。

ER図の書き方

それでは、実際にER図を書いていきます。

①データベースに入れたい情報を洗い出す

まずは、自分の成果物に必要な情報を書き出していきます。ここでは、「カテゴリーを選んで投稿ができるシンプルなブログアプリ」を想定して、ER図を作成します。
ac63bf0e-0dc5-11ee-83b0-06720a606bea.png?ref=thumb

②draw.io(diagrams.net)を使いER図を書く

必要な情報を書き出せたら、drow.ioを使ってER図を書いていきます。

⑴「ファイル→新規作成→ER図→作成する」の順で、新しいファイルを作成します。
a4fed746-f3c4-11ed-9301-06720a606bea.png?ref=thumb
以下のように、デフォルトで表示される図形を削除しましょう。
af98c946-f3c4-11ed-9cb7-064017533d40.png?ref=thumb

⑵次に、メニューバーの「ER」から最左上のテーブルを選択し、画面上にテーブルを作成します。
0f82677c-f3c5-11ed-9fb7-06720a606bea.png?ref=thumb

⑶①で書き出した情報を元に、ER図を作成していきます。カラムの追加・削除は、画面上部のメニューバーから行えます。
132869da-f3c5-11ed-a07f-06720a606bea.png?ref=thumb

⑷すべてのテーブルが完成したら、リレーションを記述します。1対多のリレーションを記述する際は、片側が3本足になっている線を使用し、1本足側が「1」、3本足側が「多」を表します。画面左のメニューから、片側3本足の線を選択しましょう。他にも多くの種類の線がありますが、今回はこちらのみ使います。
0deb911a-0dcc-11ee-a15c-061da1ef3444.png?ref=thumb

⑸画像のように、リレーション先が3本足になるように、該当するテーブル同士を繋ぎます。すべてのリレーションが描けたら完成です。
f395b2a0-0dcb-11ee-ad2a-064017533d40.png?ref=thumb

draw.ioの共有方法に関しては、こちらの資料の「draw.ioの共有方法」を参照してください。

参考〜マイグレーションファイル〜

今回作成したER図をもとに、マイグレーションファイルを定義してみましょう。マイグレーションについてはこちらを参照してください。

usersテーブルのマイグレーション
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
};
categoriesテーブルのマイグレーション
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
  Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
};
postsテーブルのマイグレーション
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained();
            $table->foreignId('category_id')->constrained();
            $table->string('title', 50);
            $table->string('post', 200);
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};
commentsテーブルのマイグレーション
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained();
            $table->foreignId('post_id')->constrained();
            $table->string('comment', 200);
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('comments');
    }
};