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.
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_statuslists the values a status can have.- Each
Tableblock lists its columns, one per line. A column is a name, a type, and settings in square brackets. [pk, increment]makesidthe primary key, numbered by the database.ref: > customers.idmakesorders.customer_idpoint atcustomers.id. That's many orders to one customer.statushas the enum's name as its type, and starts as'pending'.
The rules that matter
- 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. - 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]. - Every name a relationship uses must exist. A
refto a table or column that isn't in the code is a mistake, and the pane says which. - Text goes in single quotes, names in double quotes.
default: 'pending'andNote: 'One row per order'are text. A name with spaces or symbols goes in double quotes, such as"order items". - Keywords ignore case, names don't.
tableworks likeTable, andNOT NULLlikenot null. ButUsersandusersare 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.columnafter a relationship's operator;- table names inside a group.
The rest of the code
Each part of schema code has its own page:
- Tables and keys covers columns, types, settings, keys, indexes, checks and schemas.
- Relationships covers
ref:,Ref:and the operators. - Enums, groups and notes covers those three blocks.
- Colours covers
headercolorandcolor. - Names and ids covers quotes, and how elements keep their ids.
- Mistakes says what each message in the pane means.
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.