Schematex

ERD (Entity-Relationship Diagram)

About ERDs

An Entity-Relationship Diagram (ERD) documents the structure of a relational database: tables (entities), their columns (attributes), and the foreign-key relationships between them — including cardinality (1..1 / 0..1 / 1..N / 0..N). Backend engineers draw them to onboard new teammates onto a codebase. Data architects sketch them on a whiteboard before writing any DDL. Database educators (Elmasri & Navathe; Silberschatz / Korth / Sudarshan) put them at the centre of the conceptual-design unit in every CS database course.

Schematex implements crow's-foot notation — the de-facto modern style used by MySQL Workbench, dbdiagram.io, ERDPlus, Lucidchart, draw.io, Mermaid erDiagram, and Oracle SQL Developer Data Modeler. The DSL is DBML-compatible-ish so engineers can copy-paste; it also accepts the Mermaid }o--|| ASCII glyphs as input aliases for users coming from Mermaid. The Chen 1976 notation (rectangles + diamonds + ovals + ISA hierarchies) and Barker overlay are deferred to v0.2; today, every diagram is rendered as crow's foot.

Note — This is not the same as the entity engine. entity (entity structure) is for corporate / legal ownership hierarchies (subsidiaries, percentage rollup, trusts). erd is for database schemas (tables, columns, foreign keys). Different domain, different layout, different audience.

erd·§
↘ preview
100%
University Schema Entity-Relationship Diagram with 4 entities and 3 relationships. University Schema majors in Major major_id int PK name varchar Course course_id int PK title varchar credits int Student student_id int PK name varchar email varchar UK major_id int FK Enrollment student_id int PK FK course_id int PK FK grade char
UTF-8 · LF · 30 lines · 674 chars✓ parsed·6.7 ms·8.1 KB SVG

1. Your first ERD

The smallest useful ERD: a parent table referenced by a child via foreign key.

erd·§
↘ preview
100%
Schematex ERD Entity-Relationship Diagram with 2 entities and 1 relationships. places Customer customer_id int PK email varchar UK Order order_id int PK customer_id int FK
UTF-8 · LF · 10 lines · 252 chars✓ parsed·0.9 ms·4.3 KB SVG

Four rules cover 80 % of usage:

  1. Start with erd. Optional headers title:, direction: LR | TB.
  2. Each table is a block: table Name { col type marker }. Markers are PK, FK, UK, NN (or *) for NOT NULL.
  3. Inline foreign-key target: append FK -> Other.column to a column. The renderer adds an FK pill automatically.
  4. Connect tables with a ref line: ref Source <left-card> -- <right-card> Target [: "label"]. Cardinality is one of one-mandatory, one-optional, many-mandatory, many-optional (named form), 1..1 / 0..1 / 1..N / 0..N (Min-Max), or Mermaid glyphs (}o--||, etc.) as alias.

Comments use // or #. Block forms can be either multi-line (one column per line) or inline (table A { id int PK; name varchar }).


2. Cardinality glyphs

Crow's foot encodes the cardinality at each end of the relationship line:

GlyphReadingMin..MaxNamed token
─┃ (perpendicular bar)Exactly one (mandatory one)1..1one-mandatory
─○ (open circle)Zero or one (optional one)0..1one-optional
─┃< (bar + crow's foot)One or more (mandatory many)1..Nmany-mandatory
─○< (circle + crow's foot)Zero or more (optional many)0..Nmany-optional

Each end of a line is annotated independently. A typical 1:N relationship reads "exactly one CUSTOMER places zero-or-more ORDERs":

ref Order.customer_id many-mandatory -- one-mandatory Customer.customer_id : "places"

Reading right-to-left: the right end of the line attaches to Customer with a single bar (one-mandatory); the left end attaches to Order with a bar + crow's foot (many-mandatory).


3. Mermaid alias

If you already use Mermaid erDiagram, the same glyphs work as input:

erd·§
↘ preview
100%
Schematex ERD Entity-Relationship Diagram with 3 entities and 2 relationships. places contains Customer customer_id int PK email varchar UK Order order_id int PK customer_id int FK Product product_id int PK name varchar
UTF-8 · LF · 7 lines · 266 chars✓ parsed·1.5 ms·5.8 KB SVG
Mermaid tokenSchematex meaning
|o left / o| right0..1
||1..1
}o left / o{ right0..N
}| left / |{ right1..N
--identifying / solid line
..non-identifying / dashed line

4. Identifying vs non-identifying

Use -- for identifying relationships (solid line) and .. for non-identifying (dashed):

erd·§
↘ preview
100%
Schematex ERD Entity-Relationship Diagram with 2 entities and 1 relationships. logs (optional FK) User id int PK email varchar SessionLog id int PK user_id int FK created_at timestamp
UTF-8 · LF · 5 lines · 214 chars✓ parsed·1.5 ms·4.4 KB SVG

The dashed line follows the Barker / IDEF1X non-identifying convention.


5. Composite primary keys (associative tables)

Associative ("junction") tables resolve M:N relationships. Mark each PK + FK column with both PK and FK:

erd·§
↘ preview
100%
Schematex ERD Entity-Relationship Diagram with 3 entities and 2 relationships. Student student_id int PK name varchar Course course_id int PK title varchar Enrollment student_id int PK FK course_id int PK FK grade char
UTF-8 · LF · 10 lines · 381 chars✓ parsed·1.1 ms·5.7 KB SVG

Schematex renders both pills (PK + FK) on the same row. The PK underline applies to the column name when any PK marker is present.


6. Layout direction

direction: LR (default) places parent tables left, child tables right. direction: TB flips to top-to-bottom — useful for narrow embeds:

erd·§
↘ preview
100%
Library (top-down) Entity-Relationship Diagram with 3 entities and 2 relationships. Library (top-down) borrowed by of Member id int PK name varchar email varchar UK Book id int PK title varchar author varchar Loan id int PK member_id int FK book_id int FK due_date date
UTF-8 · LF · 10 lines · 409 chars✓ parsed·7.4 ms·6.7 KB SVG

Layered orthogonal Manhattan routing with single-bend edges. v0.1 uses appearance-order stacking inside layers; barycenter crossing-reduction is deferred to v0.2.


7. Notation modes

The DSL header can pin the notation:

erd
notation: crowsfoot     // default — only mode supported in v0.1

notation: chen (rectangle-diamond-oval, weak entities, ternary, ISA) and notation: barker (per-half dashed) are documented in 27-ERD-STANDARD.md but rejected by the parser today. Targets v0.2.


8. Limitations of v0.1

  • Crow's foot only. Chen and Barker rendering deferred.
  • No edge-crossing minimization. Layered placement uses declaration order within layers; large ERDs (10+ tables, dense FKs) will show some crossings. Reorder table blocks if a specific layout matters.
  • No self-referential C-loops. Recursive relationships (Employee.manager_id -> Employee.id) parse but route as straight orthogonal lines, not the canonical C-shape.
  • No M:N auto-resolution. Express the associative entity explicitly (this is the standard practice in production schemas anyway).

For the full standard reference, see docs/reference/27-ERD-STANDARD.md.