SQL Tables
Basics
You can easily diagram entity-relationship diagrams (ERDs) in D2 by using the sql_table
shape. Here's a minimal example:

Each key of a SQL Table shape defines a row. The primary value of each row defines its type.
The constraint value of each row defines its SQL constraint. D2 will recognize and shorten:
constraint | short |
---|---|
primary_key | PK |
foreign_key | FK |
unique | UNQ |
But you can set any constraint you'd like. It just won't be shortened if unrecognized.
Foreign Keys
Here's an example of how you'd define a foreign key connection between two tables:

If you hover over the primary key or foreign key with your cursor, you'll notice that the corresponding key is highlighted.
Example
Like all other shapes, you can nest sql_tables
into containers and define edges
to them from other shapes. Here's an example:
