Skip to main content

SQL Tables

Basics

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

sql table render

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:

constraintshort
primary_keyPK
foreign_keyFK
uniqueUNQ

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:

sql table render

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:

sql table render