DBML cheat sheet: the whole syntax on one page
Published
DBML (Database Markup Language) is small enough to hold in your head, and most of it is on this page. Each section is one construct with an example you can copy. Every example here is checked against the DBML parser, so what you paste will parse.
New to the language? What is DBML? explains what it is for. To see any of these drawn, paste them into the DBML editor.
Tables and columns
A table is a name and a block of columns. A column is a name, then a type. The type is not checked against any list: write whatever your database calls it.
Table users {
id integer
email varchar(255)
created_at timestamp
}Column settings
Settings go in square brackets after the type, separated by commas. A type with a space in it,
such as double precision, goes in double quotes.
Table products {
id integer [pk, increment]
sku varchar(64) [not null, unique]
name varchar(200) [not null]
price numeric(10,2) [not null, default: 0]
status varchar(20) [default: 'draft']
is_active boolean [default: true]
created_at timestamp [default: `now()`]
deleted_at timestamp [null]
weight "double precision" [note: 'In kilograms']
}| Setting | Meaning |
|---|---|
pk | Primary key. primary key is the long form. |
increment | Auto-incrementing. |
not null / null | Whether the column may be empty. |
unique | No two rows share a value. |
default: | A number, a 'string', true / false / null, or an expression in backticks. |
note: | A description of the column, in single quotes. |
ref: | A relationship from this column (next section). |
Relationships
The shortest way to declare a foreign key is on the column itself, with ref::
Table orders {
id integer [pk]
user_id integer [not null, ref: > users.id]
}The same relationship can stand on its own line, which keeps table blocks short and is the form to use when the two tables are in different files:
Ref: orders.user_id > users.idThe symbol between the two columns says how many rows sit on each side:
// many orders to one user
Ref: orders.user_id > users.id
// one user to one profile
Ref: profiles.user_id - users.id
// many posts to many tags
Ref: posts.id <> tags.id| Symbol | Reads as |
|---|---|
> | Many to one. The left column is the foreign key. |
< | One to many. The right column is the foreign key. |
- | One to one. |
<> | Many to many. |
For when to use which, see one-to-many vs many-to-many relationships.
Naming a relationship and setting its actions
A name goes after Ref. What happens to the referencing rows on delete or update
goes in brackets at the end: cascade, restrict, set null, set default, or no action.
Ref fk_orders_user: orders.user_id > users.id [delete: cascade, update: no action]Composite keys
A key of more than one column is declared in the table’s Indexes block, and a
reference to it lists the columns in parentheses on both sides:
Table order_items {
order_id integer
line_no integer
sku varchar
Indexes {
(order_id, line_no) [pk]
}
}
Table shipments {
id integer [pk]
order_id integer
line_no integer
}
Ref: shipments.(order_id, line_no) > order_items.(order_id, line_no)Indexes
Indexes live in an Indexes block inside the table, one per line. A line is a
column, several columns in parentheses, or an expression in backticks, followed by optional
settings: unique, pk, name:, and type:.
Table bookings {
id integer [pk]
room_id integer
guest_email varchar
starts_on date
status varchar
Indexes {
room_id
(room_id, starts_on) [unique]
status [name: 'bookings_status_idx']
guest_email [type: hash]
`lower(guest_email)` [name: 'bookings_guest_lower']
}
}Enums
An enum is declared once and used as a column type. Each value can carry its own note.
Enum order_status {
pending
paid
shipped [note: 'Handed to the carrier']
cancelled
}
Table orders {
id integer [pk]
status order_status [not null, default: 'pending']
}Notes
A column note is the note: setting. A table note is a Note: line
inside the table. Triple quotes hold a note that runs over several lines.
Table invoices {
id integer [pk]
total numeric(10,2) [note: 'Snapshotted at issue time']
Note: 'One row per invoice. Never updated after it is sent.'
}
Table audit_log {
id integer [pk]
Note: '''
Append-only.
Rows are written by a trigger, never by the application.
'''
}Schemas
A table in a database schema other than the default is written with the schema in front. A reference into it uses the same qualified name.
Table core.accounts {
id integer [pk]
}
Table billing.invoices {
id integer [pk]
account_id integer [not null, ref: > core.accounts.id]
}Table groups and the project block
A TableGroup names a set of tables that belong together. A Project block describes the schema as a whole, and a schema has at most one.
TableGroup checkout {
orders
users
}Project shop {
database_type: 'PostgreSQL'
Note: 'The storefront schema'
}Comments
Comments are for whoever reads the file, and are not part of the schema. A note, by contrast, is part of it and travels with the table.
// A line comment
/*
A block comment,
over several lines
*/
Table sessions {
id integer [pk] // comments can trail a line
}From DBML to a database, and back
DBML describes a schema; it does not create one. To turn a file into CREATE TABLE statements, use the DBML to SQL converter, and to get DBML out of a database you
already have, the SQL to DBML converter. Both handle PostgreSQL,
MySQL, and SQL Server.