Schema code

Write an ER diagram as text, one block per table, and learn the rules every schema follows.

Schema code is the text form of an ER diagram. The Schema code pane beside the canvas shows it, and you can edit it there. The diagram follows when you pause typing.

The assistant and a connected AI app write the same code, so knowing it helps you read what they draft. The layout is automatic, so the code only says what the tables hold and how they relate.

Schema code is DBML, the language dbdiagram.io uses.

A first schema

Paste this into the Schema code pane to see it drawn.

Schema code
Project shop {
  database_type: 'PostgreSQL'
  Note: 'What the shop sells and who bought what'
}

Enum order_status {
  pending
  paid
  shipped
}

Table customers {
  id integer [pk, increment]
  email varchar(255) [unique, not null]
  name varchar(120) [not null]
}

Table orders {
  id integer [pk, increment]
  customer_id integer [not null, ref: > customers.id]
  status order_status [not null, default: 'pending']
  placed_at timestamp [not null, default: `now()`]
}

Reading it from the top:

  • Project shop { … } names the diagram, says which database it's for, and adds a note.
  • Enum order_status lists the values a status can have.
  • Each Table block lists its columns, one per line. A column is a name, a type, and settings in square brackets.
  • [pk, increment] makes id the primary key, numbered by the database.
  • ref: > customers.id makes orders.customer_id point at customers.id. That's many orders to one customer.
  • status has the enum's name as its type, and starts as 'pending'.

The rules that matter

  1. Blocks open with { and close with }. Tables, enums, groups, notes and the Project are each a block. The app writes each line of a block on a line of its own, and the closing } too.
  2. One column per line. A column is a name, then a type, then optional settings in square brackets, separated by commas: email varchar(255) [unique, not null].
  3. Every name a relationship uses must exist. A ref to a table or column that isn't in the code is a mistake, and the pane says which.
  4. Text goes in single quotes, names in double quotes. default: 'pending' and Note: 'One row per order' are text. A name with spaces or symbols goes in double quotes, such as "order items".
  5. Keywords ignore case, names don't. table works like Table, and NOT NULL like not null. But Users and users are two different tables.

The Project block

Project shop {
  database_type: 'PostgreSQL'
  Note: 'What the shop sells and who bought what'
}

The Project block is optional, and there's at most one. Its name is the diagram's title.

database_type says which database the schema is for, such as 'PostgreSQL', 'MySQL', 'SQL Server' or 'Oracle'. The assistant writes types for it. Note says what the schema is for.

In the app, these are Title, Database and Note in Properties when nothing is selected.

Comments

// Billing tables come later
Table customers {
  id integer [pk, increment]
}

A line that starts with // is a comment. The app ignores it. It keeps the comment in the text when the diagram changes elsewhere, on the canvas or by the assistant.

As you type

The pane suggests what can come next:

  • the block keywords at the start of a line;
  • a type after a column's name, your enums included;
  • the settings inside square brackets;
  • table.column after a relationship's operator;
  • table names inside a group.

The rest of the code

Each part of schema code has its own page:

To bring a schema in from a SQL, Mermaid or DBML file, or to download one, see Import a schema and Export.

Full reference

The schema code reference has the complete syntax on one page, exactly as the assistant gets it, with the reference schemas it draws from.

On this page