Documentation
Syntax
Relationship

Relationship expression

To define a relationship between two or more entities we use the keyword relation, followed by the name of the relationship and its partipating entities with their constraints. Relationships may also have attributes. The following example shows the relationship "Drinks", which has the attribute "amount", between the entities "Human" and "Coffee". The entity "Human" has a cardinality of 1, so the relationship is many-to-one:

entity Human {
  id key
  name
  age
}
 
entity Coffee {
  id key
  species
  roast_date
}
 
relation Drinks(Human 1, Coffee) {
  amount
}

The example above generates the following ER Diagram (using the arrow notation from the Playground):

Example relationship ER Diagram

Participating entities

Participating entities are the entities that are part of the relationship. ERdoc supports n-ary relationships, so we can have as many participants as we want. note that an Aggregation can also be a participant. Relationship participants are declared with the (participant1, participant2, ..., participantN) syntax, e.g:

relation commutesTo(Student, Professor, University)

Cardinality and Participation constraints

We can specify the cardinality of a relationship by adding a number or a single, capital letter after the participant name. For participation constraints, the ! character denotes that the participant has total participation in the relationship, if ! is not present, the participant has partial participation. The following table shows common cardinality and participation constraints in ERdoc syntax, and their (min, max) equivalents:

Constraint(min, max)
1(0, 1)
N(0, N)
1!(1, 1)
N!(1, N)
💡

If no constraints are specified, the participant will have a cardinality of N and partial participation in the relationship. This is equivalent as explicitly writing N.

The following is a ternary relationship where the participants have different constraints:

entity Supplier {
  id key
}
 
entity Project {
  pname key
}
 
entity Part {
  part_no key
}
 
relation Supply(Supplier 1, Project M, Part N!) {
  amount
}

and the ERD:

ternary relationship ER Diagram

Participant with roles

A relationship participant can also participate more than once in the same relationship instance, this is called a role. To specify a role, we use a syntax similar to the one for composite attributes. Each "role" can have its own cardinality and participation constraints. The following example shows a single entity, "Employee", participating with the roles "Intern" and "Manager" in the relationship "Manages":

entity Employee {
  id key
  name
  age
}
 
relation Manages(Employee: [Intern 1, Manager])

In the ERD, roles are visualized as labeled edges:

relationship with roles ER Diagram

Note that you can have other participants in a relationship, and they can also have roles.

💡

Even though the ERdoc language supports an arbitrary number of roles, the ERDs generated by the Playground support up to 5 roles, if there's more, only the first 5 will be shown. This is because it's hard to position the handles of the relationship and entity nodes so that the edges look good (PRs are welcome!).