データベース設計において欠かせないER図。PlantUMLを使えば、テキストベースで簡単にER図を作成できます。本記事では、PlantUMLでER図を作成する方法を網羅的に解説します。エンティティの定義から関係性の表現まで、具体的なサンプルコードとともに紹介していきます。
PlantUMLとは?
PlantUMLは、シンプルなテキスト記述でUMLダイアグラムやフローチャートを作成できるツールです。 専用の構文を覚える必要はありますが、一度慣れてしまえば、GUIツールよりも素早く、そして美しくフローチャートを描けるようになります。 バージョン管理システムとの相性も抜群で、テキストベースなので変更履歴の追跡も簡単です。
UML・PlantUMLや、VSCode環境への導入方法等は以下にまとめていますので、こちらを参照ください。
エンティティの定義
エンティティはデータベースのテーブルを表します。PlantUMLでは以下のように定義します。
@startuml
entity "ユーザー" as user {
+ ユーザーID : integer
--
ユーザー名 : varchar(50)
メールアドレス : varchar(100)
作成日時 : datetime
}
@enduml

アトリビュート(属性)の表現
アトリビュートはエンティティの属性を表します。主キーや外部キーなども表現できます。
@startuml
entity "注文" as order {
+ 注文ID : integer <<PK>>
--
# ユーザーID : integer <<FK>>
注文日時 : datetime
合計金額 : decimal(10,2)
}
@enduml

リレーションシップの表現
エンティティ間の関係性を表現するリレーションシップは、線と記号で表します。
@startuml
entity "ユーザー" as user
entity "注文" as order
user ||--o{ order : 発注する
@enduml

カーディナリティの表現
カーディナリティ(多重度)は、エンティティ間の数量関係を表します。
@startuml
entity "著者" as author
entity "書籍" as book
author "1" -- "0..*" book : 執筆する
@enduml

パッケージによるグループ化
関連するエンティティをパッケージでグループ化できます。
@startuml
package "ユーザー管理" {
entity "ユーザー" as user
entity "プロフィール" as profile
}
package "商品管理" {
entity "商品" as product
entity "カテゴリ" as category
}
user ||--|| profile
product }o--|| category
@enduml

注釈の追加
ER図に注釈を追加して、noteを使用して補足情報を記載できます。
@startuml
entity "ユーザー" as user@startuml
entity "ユーザー" as User {
+ ユーザーID : integer <<PK>>
--
ユーザー名 : varchar(50)
メールアドレス : varchar(100)
}
entity "注文" as Order {
+ 注文ID : integer <<PK>>
--
# ユーザーID : integer <<FK>>
注文日 : date
合計金額 : decimal(10,2)
}
User ||--o{ Order
note right of User : ユーザー情報を管理する\nエンティティです。
note bottom of Order : 注文情報を保持します。\n各注文は必ずユーザーに\n紐づいています。
note "1人のユーザーは\n0以上の注文を\n持つことができます" as N1
User .. N1
N1 .. Order
@enduml
note right of user
ユーザー情報を管理する
テーブルです。
end note
@enduml

まとめ
PlantUMLを使用すると、テキストベースでER図を簡単に作成できます。エンティティの定義、アトリビュートの表現、リレーションシップの設定、カーディナリティの指定など、ER図に必要な要素を全て表現できます。さらに、パッケージによるグループ化や注釈の追加により、より詳細で分かりやすいER図を作成することができます。テキストベースで作成できるため、バージョン管理も容易で、チーム開発にも適しています。ER図作成の効率化に、ぜひPlantUMLを活用してみてください。
参考情報
ER図についてより詳細や機能については、以下を参照ください。
https://plantuml.com/ja-dark/ie-diagram
この記事が、あなたの役に立てば幸いです!
コメント