Draw entity-relationship models/diagrams in plain text
What is a entity-relationship diagram? Per Entity–relationship model - Wikipedia - en.wikipedia.org:
An entity–relationship model (or ER model) describes interrelated things of interest in a specific domain of knowledge. A basic ER model is composed of entity types (which classify the things of interest) and specifies relationships that can exist between entities (instances of those entity types).
If you are designing a database, or that you're taking over a new project, you may need ERD to better understand the system. You can do it with a pen and a piece of a paper, or you can use this ERD app to "draw" ER diagrams with plain text.
Interesting, you may wonder, so why not go to ERD to play around.
It has simple syntax that looks like ini, and it's about:
- comments with # this is a comment
- entities and relationships
- labels
- other global directives
- fonts and colors
Entities
Entities are declared inside [
and ]
, it has this format:
[`Entity name`]
attribute1
attribute2
If the entity names and attributes contain spaces or other non-alphanumeric characters, quote them with backticks, simple quotes or double quotes.
Some examples:
- A entity with no attributes, for example,
[App]
-
A entity with 2 attributes:
[`App Table`] Id Name Desc
-
Prefix attributes of the primary key with a
*
, and a foreign key with+
[`App DB Table`] *Id Name Desc +Creator
Relationships
Every relationship includes exactly two entities (the two entities may be the same, for self-relationships).
There 4 cardinalities:
Cardinality Syntax
0 or 1 ?
exactly 1 1
0 or more *
1 or more +
If you're familiar with Regular Expressions, you may have noticed that the syntax is almost the same. For example, App *--1 Creator
means one creator could create >=0 applications.
You can declare relationships anywhere in the content.
Global directives
Global special directives:
-
title
for specifying the diagram title and formatting options, e.g.title {label: "ERD demo"}
-
entity
for specifying options for all entities, e.g.entity {bgcolor: "#ececfc", size: "20"}
-
relationship
for all relationships
Global options are overwritten by local options.
Formating options
label
, label for a diagram/entity/attributecolor
, foreground colorbgcolor
, background colorsize
, font sizeborder-color
, border colorborder
, border size
Colors are in hexadecimal notation, such as, #ff2233
, and they can also be english names like red
/black
.
An complete example
This example comes from https://github.com/BurntSushi/erd
title {label: "nfldb Entity-Relationship diagram (condensed)", size: "20"}
# Entities
[player] {bgcolor: "#d0e0d0"}
*player_id {label: "varchar, not null"}
full_name {label: "varchar, null"}
team {label: "varchar, not null"}
position {label: "player_pos, not null"}
status {label: "player_status, not null"}
[team] {bgcolor: "#d0e0d0"}
*team_id {label: "varchar, not null"}
city {label: "varchar, not null"}
name {label: "varchar, not null"}
[game] {bgcolor: "#ececfc"}
*gsis_id {label: "gameid, not null"}
start_time {label: "utctime, not null"}
week {label: "usmallint, not null"}
season_year {label: "usmallint, not null"}
season_type {label: "season_phase, not null"}
finished {label: "boolean, not null"}
home_team {label: "varchar, not null"}
home_score {label: "usmallint, not null"}
away_team {label: "varchar, not null"}
away_score {label: "usmallint, not null"}
[drive] {bgcolor: "#ececfc"}
*+gsis_id {label: "gameid, not null"}
*drive_id {label: "usmallint, not null"}
start_field {label: "field_pos, null"}
start_time {label: "game_time, not null"}
end_field {label: "field_pos, null"}
end_time {label: "game_time, not null"}
pos_team {label: "varchar, not null"}
pos_time {label: "pos_period, null"}
[play] {bgcolor: "#ececfc"}
*+gsis_id {label: "gameid, not null"}
*+drive_id {label: "usmallint, not null"}
*play_id {label: "usmallint, not null"}
time {label: "game_time, not null"}
pos_team {label: "varchar, not null"}
yardline {label: "field_pos, null"}
down {label: "smallint, null"}
yards_to_go {label: "smallint, null"}
[play_player] {bgcolor: "#ececfc"}
*+gsis_id {label: "gameid, not null"}
*+drive_id {label: "usmallint, not null"}
*+play_id {label: "usmallint, not null"}
*+player_id {label: "varchar, not null"}
team {label: "varchar, not null"}
[meta] {bgcolor: "#fcecec"}
version {label: "smallint, null"}
season_type {label: "season_phase, null"}
season_year {label: "usmallint, null"}
week {label: "usmallint, null"}
# Relationships
player *--1 team
game *--1 team {label: "home"}
game *--1 team {label: "away"}
drive *--1 team
play *--1 team
play_player *--1 team
game 1--* drive
game 1--* play
game 1--* play_player
drive 1--* play
drive 1--* play_player
play 1--* play_player
player 1--* play_player
Go to play with ERD, and have fun!