ER basics

What an ER diagram shows, the parts it's made of, when to draw one, and how to start one.

An ER (entity-relationship) diagram shows the tables of a database. It shows the columns each table holds, its keys, and how the tables relate to each other.

Use one to design a new schema, to agree on one before anyone writes a migration, or to explain an existing database to someone new.

Each element below comes with the schema code that draws it.

Tables and columns

A table is a card with its name at the top and one row per column. Each row shows the column's name on the left and its type on the right.

A few marks tell you more about a column:

  • A key icon and a name in bold mean the column is part of the table's primary key.
  • A link icon means the column points at another table. It's a foreign key.
  • UQ next to the type means no two rows can have the same value.
  • A ? after the type means the column can be empty. Columns without it always need a value.
Schema code
Table customers {
  id integer [pk, increment]
  email varchar(255) [unique, not null]
  name varchar(120) [not null]
  phone varchar(30)
}

Name tables as plural nouns and columns in lower case with underscores, such as orders and customer_id. The assistant follows the same style.

Keys

The primary key is the column, or set of columns, that picks out exactly one row. Most tables have an id column that the database numbers on its own.

A table that joins two others often uses both of its foreign keys together as its key. One example is a table that records which people belong to which teams.

Every table should have a primary key. The checks warn about a table that has none.

Relationships

A relationship is a line from a column in one table to the key it points at in another. The ends of the line show how many rows can be on each side, in crow's foot notation:

  • At the table that holds the reference, a crow's foot means many rows can point at the same row. A bar means only one can, as in a one-to-one relationship.
  • At the table being pointed at, a bar means one. A second bar next to it means every row must have one, because the referencing column always needs a value. A circle means the reference can be empty.

There are three kinds:

KindWhat it means
Many to oneMany rows here point at one row there, such as many orders for one customer. The most common kind.
One to oneEach row here points at a different row there, such as a profile for a user. The referencing column should be unique.
Many to manyRows on each side relate to any number on the other. Databases can't store this directly, so the checks ask for a junction table: a table with a foreign key to each side.
Schema code
Table customers {
  id integer [pk, increment]
  name varchar(120) [not null]
}

Table orders {
  id integer [pk, increment]
  customer_id integer [not null, ref: > customers.id]
  total decimal(10,2) [not null]
}

Select a relationship and the Properties panel says what it means in a sentence, such as "Each row in orders has exactly one row in customers; each row in customers has any number of rows in orders."

Enums

An enum is a type with a fixed list of values, such as the states an order goes through. The app draws it as a dashed card below the tables, marked enum, with one value per row.

A column uses the enum by giving its name as the column's type.

Schema code
Enum order_status {
  pending
  paid
  shipped
}

Table orders {
  id integer [pk, increment]
  status order_status [not null, default: 'pending']
}

Use an enum for a short list that rarely changes. When people edit the list, or each value carries data of its own, use a table instead.

Groups and notes

A group is a frame around tables that belong to one area of the system, such as billing or the catalog. A table can be in one group at most. Groups help once a schema has more than about eight tables.

Schema code
Table invoices {
  id integer [pk, increment]
  number varchar(30) [unique, not null]
}

Table invoice_lines {
  id integer [pk, increment]
  invoice_id integer [not null, ref: > invoices.id]
  amount decimal(12,2) [not null]
}

TableGroup billing {
  invoices
  invoice_lines
}

A note says what the names can't. It can say what one row of a table is, what a column holds, or what the whole schema is for.

Tables, columns, groups and the schema itself can each have a note. A table with a note shows a note icon in its header, and a column shows one after its name. Point at the icon to read the note.

A note can also stand on its own. The app draws it as a card below the tables, next to the enums.

Colour

Colour marks what belongs together or needs a second look, such as the tables of one area or the line to a legacy table.

A table's or an enum's header, a group's frame, a note and a relationship's line each take one of eight colours. Names and types read the same in every colour. Columns have no colour.

To set a colour, see Colour. The assistant colours things only when you ask. The code writes colour as headercolor and color, as Colours shows.

When to use an ER diagram

Each kind of diagram answers a different question:

  • A BPMN process answers "what work happens, who does it, and what comes next".
  • A sequence diagram answers "what do these systems say to each other, and in what order".
  • A flowchart answers "what happens next, and what if not".
  • An ER diagram answers "what do we store, and how does it fit together".

They work well side by side. A step in a process, such as "Record the order", writes rows to the tables an ER diagram shows. A database in a sequence diagram holds them.

Keep each ER diagram to one system at one level of detail, about 3 to 25 tables. Leave out columns nobody has asked for yet. Once there are more than about eight tables, group them by area.

Make an ER diagram

  • In the Diagrams panel, select New diagram (the + button) and choose ER diagram.
  • Press ⌘ K (Ctrl K on Windows and Linux) and run New ER diagram.
  • Select Templates in the rail. Under ER diagram are Online store and Team workspace, and View all adds Blog and Appointments. Selecting one creates a new diagram from it.
  • In the Diagrams panel, open New diagram and choose Import SQL, DBML or Mermaid file…. See Import a file.

A new diagram is called "Untitled schema". Its empty canvas offers three ways to begin:

  • Describe it opens the assistant.
  • Add table adds the first table.
  • Use a template shows the ER templates.

You can also type the schema in the Schema code pane beside the canvas. For what's the same in every kind, see Start a diagram.

Next

Learn to edit on the canvas or write schema code, or let the assistant design a schema for you.

On this page