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:

my_tableidintPKlast_updatedtimestamp with time zone

Each key of a SQL Table shape defines a row. The primary value (the thing after the colon) 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.

info

You can also specify multiple constraints with an array.

Escaping reserved keywords

If you'd like to use a reserved keyword, wrap it in quotes.

Foreign Keys

Here's an example of how you'd define a foreign key connection between two tables:

objectsidintPKdiskintFKjsonjsonbUNQlast_updatedtimestamp with time zonedisksidintPK
info

When rendered with the TALA layout engine or ELK layout engine, connections point to the exact row.

Example

Like all other shapes, you can nest sql_tables into containers and define edges to them from other shapes. Here's an example:

clouddisksidintPKblocksidintPKdiskintFKblobAWS S3 Vancouver