ER図の書き方
- 3039
ER図とは
ER図とは、簡単にいうとDBの設計図です。テーブルやカラム、テーブル同士のリレーションを1つの図面で表します。リレーションについて復習したい方は、カリキュラム9-2を参照してください。
また、一般的に、ER図には幾つか種類がありますが、今回は簡易的に作成します。
命名規則について
【テーブル設計をする際の命名規則】
- 小文字の複数形
- 複数単語を繋げる場合はスネーク記法(単語と単語の間をアンダーバーで繋げる)
- 中間テーブルの場合は、テーブル名の単数系_テーブル名の単数系(アルファベット順)
ex.)postsテーブルとusersテーブルの中間テーブルの場合、post_userテーブルとなる
主キーと外部キーの記載方法〜PKとFK〜
ER 図において、
- PK=主キー
- FK =外部キー
の意味で用いられます。
ER図の書き方
それでは、実際にER図を書いていきます。
①データベースに入れたい情報を洗い出す
まずは、自分の成果物に必要な情報を書き出していきます。ここでは、「カテゴリーを選んで投稿ができるシンプルなブログアプリ」を想定して、ER図を作成します。
②draw.io(diagrams.net)を使いER図を書く
必要な情報を書き出せたら、drow.ioを使ってER図を書いていきます。
⑴「ファイル→新規作成→ER図→作成する」の順で、新しいファイルを作成します。
以下のように、デフォルトで表示される図形を削除しましょう。
⑵次に、メニューバーの「ER」から最左上のテーブルを選択し、画面上にテーブルを作成します。
⑶①で書き出した情報を元に、ER図を作成していきます。カラムの追加・削除は、画面上部のメニューバーから行えます。
⑷すべてのテーブルが完成したら、リレーションを記述します。1対多のリレーションを記述する際は、片側が3本足になっている線を使用し、1本足側が「1」、3本足側が「多」を表します。画面左のメニューから、片側3本足の線を選択しましょう。他にも多くの種類の線がありますが、今回はこちらのみ使います。
⑸画像のように、リレーション先が3本足になるように、該当するテーブル同士を繋ぎます。すべてのリレーションが描けたら完成です。
draw.ioの共有方法に関しては、こちらの資料の「draw.ioの共有方法」を参照してください。
参考〜マイグレーションファイル〜
今回作成したER図をもとに、マイグレーションファイルを定義してみましょう。マイグレーションについてはこちらを参照してください。
<?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');
}
};
Copy<?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');
}
};
Copy<?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');
}
};
Copy<?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');
}
};
Copy