データベースのダイアグラム(ER図)を作成する Haskell 製のコマンドラインツールです。テキストエディタで定義ファイルを作成し、コマンドを実行することでダイアグラムとなる画像を作成します。データベースを設計する際、テーブルとその関連付けについて全体像を手早く作成したい、そんな時に便利なソフトウェアです。

erd の主な特徴

1) データベースの全体像の設計に集中できる

ダイアグラムにあるテーブル同士の配置など気にすることなく、とにかく全体的な設計を行いたい時に便利です。GUIツールも便利ですが、使い慣れたテキストエディタで、キーボードから手を離すことなく、思うままに設計を進めることができます。

2) er ファイルが必要

er ファイルと呼ぶ定義ファイルを作成します。このer ファイルに、実体(entity)、属性(attribute)、関連(relationship)を定義します。実体はテーブル、属性はカラムを示します。以下はソースファイルに含まれる サンプルの内容になります。実体や属性の名称には2バイト文字は使用できません。

# Entities are declared in '[' ... ']'. All attributes after the entity header
# up until the end of the file (or the next entity declaration) correspond
# to this entity.
[Person]
*name
height
weight
+birth_location_id

[Location]
*id
city
state
country

# Each relationship must be between exactly two entities, which need not
# be distinct. Each entity in the relationship has exactly one of four
# possible cardinalities:
#
# Cardinality    Syntax
# 0 or 1         0
# exactly 1      1
# 0 or more      *
# 1 or more      +
Person *--1 Location

実体は[]で囲みます。属性の先頭にある * はプライマリ・キーを示し、+ は外部キーを示します。実行結果は次の通りです。

simple.er 実行結果

色やフォントサイズを指定することができます。テーブル毎に定義する場合は、エンティティの横に記述します。

[Person] {bgcolor: "#ececfc", size: "20"}
name
height
weight

定義全体で、色やフォントサイズを指定する場合は、次のように er ファイルの先頭で記述します。

entity {bgcolor: "#ececfc", size: "20"}

この他のオプションは次の通りです。

label         A plain text string used to label the item. For entity names and attributes, a label is shown next to the name in square brackets. For relationships, a label is drawn near the center of the edge. For the special title directive, the label corresponds to the graph title.
color         Specifies the font color. Valid everywhere.
bgcolor       Specifies the background color. Only valid for entities and attributes.
size          Specifies the font size. Valid everywhere.
font          Specifies the font. Valid everywhere. See this and this for information about fonts in GraphViz. TL;DR: Stick with one of the following: Times-Roman, Helvetica or Courier.
border-color  Border color. Only works for entities or attributes.
border        Border size in pixels. Only works for entities and attributes.

3)タイトルを付けられる

ダイアグラムにタイトルをつけることができます。次のように er ファイルの先頭に記述します。

title {label: "nfldb Entity-Relationship diagram (condensed)", size: "20"}

4) 出力するイメージファイルの形式が豊富

次の形式でイメージファイルを出力できます。

pdf
svg
eps
png
jpg
plain text
dot

erd のソフトウェア要件

Haskell と GraphViz が必要です。erdインストール時に一緒にインストールできます。

erd のインストール

cabal を使って erd をインストールします。

cabal install erd

cabal そのものがインストールされていない環境であれば、Haskell Platform をインストールすることをお勧めします。Haskell PlatformはWindows/OSX/Unix/Linux版があります。cabal をインストールした後、環境によっては OS の再起動が必要です。

cabal を初めて実行した場合、以下のメッセージを出力して期待した結果を得られません。

Warning: The package list for 'hackage.haskell.org' does not exist. Run 'cabal
update' to download it.
cabal: There is no package named 'erd'.
You may need to run 'cabal update' to get the latest list of available
packages.

次の順に、実行すれば、問題は解決します。

cabal update
cabal install erd

erd の使い方

ソースファイルに含まれている、もう一つの er ファイル nfldb.er を例にします。次の通りに実行します。

erd -i nfldb.er.er -o nfldb.er.png

実行結果は次の通りです。

nfldb.png erd実行結果

イメージファイルの形式は、コマンドライン・オプションで指定できますが、-o オプションと共に指定するファイル名の拡張子で自動的に判断します。-o 以外にも次の通りオプションがあります。

: erd --help
Usage: erd [flags]
  -i FILE  --input=FILE   When set, input will be read from the given file.
                          Otherwise, stdin will be used.
  -o FILE  --output=FILE  When set, output will be written to the given file.
                          Otherwise, stdout will be used.
                          If given and if --fmt is omitted, then the format will be
                          guessed from the file extension.
  -h       --help         Show this usage message.
  -f FMT   --fmt=FMT      Force the output format to one of:
                          bmp, dot, eps, gif, jpg, pdf, plain, png, ps, ps2, svg, tiff


:

erd を使うと開発者や利用者と一緒に設計を進めやすくなります。出力したイメージファイルをWeb上で共有し、Javascriptなどの仕組で短いサイクルで更新することで、他の人に見てもらいながら設計を進めることもできます。なお日本語は使えませんのでご注意ください。

erd は Haskell 製、Public Domainのオープンソース・ソフトウェアです。

BurntSushi/erd

source code

The Haskell Platform