主题
实体关系图
¥Entity Relationship Diagrams
实体关系模型(或 ER 模型)描述特定知识字段中相关的感兴趣的事物。基本 ER 模型由实体类型(对感兴趣的事物进行分类)组成,并指定实体(这些实体类型的实例)之间可以存在的关系 维基百科。
¥An entity–relationship model (or ER model) describes interrelated things of interest in a specific domain of knowledge. A basic ER model is composed of entity types (which classify the things of interest) and specifies relationships that can exist between entities (instances of those entity types) Wikipedia.
请注意,ER 建模的实践者几乎总是将实体类型简称为实体。例如,CUSTOMER
实体类型将简称为 CUSTOMER
实体。这种情况很常见,不建议做任何其他事情,但从技术上讲,实体是实体类型的抽象实例,这就是 ER 图所示的内容 - 抽象实例以及它们之间的关系。这就是为什么实体总是使用单数名词来命名。
¥Note that practitioners of ER modelling almost always refer to entity types simply as entities. For example the CUSTOMER
entity type would be referred to simply as the CUSTOMER
entity. This is so common it would be inadvisable to do anything else, but technically an entity is an abstract instance of an entity type, and this is what an ER diagram shows - abstract instances, and the relationships between them. This is why entities are always named using singular nouns.
Mermaid 可以绘制 ER 图
¥Mermaid can render ER diagrams
实体名称通常是大写的,尽管对此没有公认的标准,并且在 Mermaid 中也没有要求。
¥Entity names are often capitalised, although there is no accepted standard on this, and it is not required in Mermaid.
实体之间的关系由带有表示基数的结束标记的线表示。Mermaid 使用最流行的鱼尾纹表示法。鱼尾纹直观地传达了它所连接的实体的许多实例的可能性。
¥Relationships between entities are represented by lines with end markers representing cardinality. Mermaid uses the most popular crow's foot notation. The crow's foot intuitively conveys the possibility of many instances of the entity that it connects to.
ER 图可用于各种目的,从没有任何实现细节的抽象逻辑模型到关系数据库表的物理模型。在 ER 图上包含属性定义有助于理解实体的目的和含义。这些不一定需要详尽无遗;通常一小部分属性就足够了。Mermaid 允许根据其类型和名称来定义它们。
¥ER diagrams can be used for various purposes, ranging from abstract logical models devoid of any implementation details, through to physical models of relational database tables. It can be useful to include attribute definitions on ER diagrams to aid comprehension of the purpose and meaning of entities. These do not necessarily need to be exhaustive; often a small subset of attributes is enough. Mermaid allows them to be defined in terms of their type and name.
在 ER 图上包含属性时,必须决定是否包含外键作为属性。这可能取决于你尝试表示关系表结构的紧密程度。如果你的图表是一个逻辑模型,并不意味着暗示关系实现,那么最好将它们排除在外,因为关联关系已经传达了实体关联的方式。例如,JSON 数据结构可以使用数组实现一对多关系,而无需外键属性。类似地,面向对象的编程语言可以使用对集合的指针或引用。即使对于用于关系实现的模型,你也可能会认为包含外键属性会重复关系已经描述的信息,并且不会为实体添加含义。最终,这是你的选择。
¥When including attributes on ER diagrams, you must decide whether to include foreign keys as attributes. This probably depends on how closely you are trying to represent relational table structures. If your diagram is a logical model which is not meant to imply a relational implementation, then it is better to leave these out because the associative relationships already convey the way that entities are associated. For example, a JSON data structure can implement a one-to-many relationship without the need for foreign key properties, using arrays. Similarly an object-oriented programming language may use pointers or references to collections. Even for models that are intended for relational implementation, you might decide that inclusion of foreign key attributes duplicates information already portrayed by the relationships, and does not add meaning to entities. Ultimately, it's your choice.
语法
¥Syntax
实体和关系
¥Entities and Relationships
ER 图的 Mermaid 语法与 PlantUML 兼容,并带有用于标记关系的扩展。每个语句由以下部分组成:
¥Mermaid syntax for ER diagrams is compatible with PlantUML, with an extension to label the relationship. Each statement consists of the following parts:
<first-entity> [<relationship> <second-entity> : <relationship-label>]
在哪里:
¥Where:
first-entity
是实体的名称。名称必须以字母字符或下划线开头(从 v10.5.0+ 开始),并且还可以包含数字和连字符。¥
first-entity
is the name of an entity. Names must begin with an alphabetic character or an underscore (from v10.5.0+), and may also contain digits and hyphens.relationship
描述了两个实体相互关联的方式。见下文。¥
relationship
describes the way that both entities inter-relate. See below.second-entity
是另一个实体的名称。¥
second-entity
is the name of the other entity.relationship-label
从第一实体的角度描述关系。¥
relationship-label
describes the relationship from the perspective of the first entity.
例如:
¥For example:
PROPERTY ||--|{ ROOM : contains
该声明可以理解为一个属性包含一个或多个房间,而一个房间是一个且唯一一个属性的一部分。可以看到这里的标签是从第一个实体的角度来看的:属性包含房间,但房间不包含属性。当从第二实体的角度考虑时,等效标签通常很容易推断出来。(一些 ER 图从两个角度标记关系,但这在这里不受支持,并且通常是多余的)。
¥This statement can be read as a property contains one or more rooms, and a room is part of one and only one property. You can see that the label here is from the first entity's perspective: a property contains a room, but a room does not contain a property. When considered from the perspective of the second entity, the equivalent label is usually very easy to infer. (Some ER diagrams label relationships from both perspectives, but this is not supported here, and is usually superfluous).
只有声明的 first-entity
部分是强制性的。这使得显示没有关系的实体成为可能,这在图表的迭代构建过程中非常有用。如果指定了语句的任何其他部分,则所有部分都是强制性的。
¥Only the first-entity
part of a statement is mandatory. This makes it possible to show an entity with no relationships, which can be useful during iterative construction of diagrams. If any other parts of a statement are specified, then all parts are mandatory.
关系语法
¥Relationship Syntax
每个语句的 relationship
部分可以分为三个子部分:
¥The relationship
part of each statement can be broken down into three sub-components:
第一个实体相对于第二个实体的基数
¥the cardinality of the first entity with respect to the second
该关系是否赋予 'child' 实体身份
¥whether the relationship confers identity on a 'child' entity
第二个实体相对于第一个实体的基数
¥the cardinality of the second entity with respect to the first
基数是描述另一个实体有多少元素可以与相关实体相关的属性。在上面的示例中,PROPERTY
可以有一个或多个与其关联的 ROOM
实例,而 ROOM
只能与一个 PROPERTY
关联。每个基数标记中有两个字符。最外面的字符代表最大值,最里面的字符代表最小值。下表总结了可能的基数。
¥Cardinality is a property that describes how many elements of another entity can be related to the entity in question. In the above example a PROPERTY
can have one or more ROOM
instances associated to it, whereas a ROOM
can only be associated with one PROPERTY
. In each cardinality marker there are two characters. The outermost character represents a maximum value, and the innermost character represents a minimum value. The table below summarises possible cardinalities.
值(左) | 值(右) | 意义 |
---|---|---|
|o | o| | 零或一 |
|| | || | 正好一个 |
}o | o{ | 零个或多个(无上限) |
}| | |{ | 1 个或多个(无上限) |
别名
¥Aliases
值(左) | 值(右) | 别名为 |
---|---|---|
一或零 | 一或零 | 零或一 |
零或一 | 零或一 | 零或一 |
一个或多个 | 一个或多个 | 一个或多个 |
一个或多个 | 一个或多个 | 一个或多个 |
many(1) | many(1) | 一个或多个 |
1+ | 1+ | 一个或多个 |
零个或多个 | 零个或多个 | 零个或多个 |
零个或多个 | 零个或多个 | 零个或多个 |
many(0) | many(0) | 零个或多个 |
0+ | 0+ | 零个或多个 |
只有一个 | 只有一个 | 正好一个 |
1 | 1 | 正好一个 |
鉴别
¥Identification
关系可分为识别性关系或非识别性关系,分别用实线或虚线渲染。当所讨论的实体之一不能离开另一个实体而独立存在时,这一点是相关的。例如,一家为人们驾驶汽车提供保险的公司可能需要在 NAMED-DRIVER
上存储数据。在建模时,我们可能首先观察到 CAR
可以由许多 PERSON
实例驱动,而 PERSON
可以驱动许多 CAR
- 两个实体都可以独立存在,因此这是一种非识别关系,我们可以在 Mermaid 中将其指定为:PERSON }|..|{ CAR : "driver"
。请注意关系中间的两个点,这将导致在两个实体之间绘制虚线。但是,当这种多对多关系解析为两个一对多关系时,我们观察到如果没有 PERSON
和 CAR
,NAMED-DRIVER
就不可能存在 - 关系变得具有识别性,并使用连字符指定,连字符转换为实线:
¥Relationships may be classified as either identifying or non-identifying and these are rendered with either solid or dashed lines respectively. This is relevant when one of the entities in question can not have independent existence without the other. For example a firm that insures people to drive cars might need to store data on NAMED-DRIVER
s. In modelling this we might start out by observing that a CAR
can be driven by many PERSON
instances, and a PERSON
can drive many CAR
s - both entities can exist without the other, so this is a non-identifying relationship that we might specify in Mermaid as: PERSON }|..|{ CAR : "driver"
. Note the two dots in the middle of the relationship that will result in a dashed line being drawn between the two entities. But when this many-to-many relationship is resolved into two one-to-many relationships, we observe that a NAMED-DRIVER
cannot exist without both a PERSON
and a CAR
- the relationships become identifying and would be specified using hyphens, which translate to a solid line:
别名
¥Aliases
值 | 别名为 |
---|---|
to | identifying |
可选地 | 不可识别的 |
属性
¥Attributes
可以通过指定实体名称后跟包含多个 type name
对的块来定义实体的属性,其中块由开头 {
和结尾 }
分隔。属性在实体框内渲染。例如:
¥Attributes can be defined for entities by specifying the entity name followed by a block containing multiple type name
pairs, where a block is delimited by an opening {
and a closing }
. The attributes are rendered inside the entity boxes. For example:
type
值必须以字母字符开头,并且可以包含数字、连字符、下划线、圆括号和方括号。name
值遵循与 type
类似的格式,但可能以星号开头,作为指示属性是主键的另一个选项。除此之外,没有任何限制,也没有隐式的有效数据类型集。
¥The type
values must begin with an alphabetic character and may contain digits, hyphens, underscores, parentheses and square brackets. The name
values follow a similar format to type
, but may start with an asterisk as another option to indicate an attribute is a primary key. Other than that, there are no restrictions, and there is no implicit set of valid data types.
实体名称别名 (v10.5.0+)
¥Entity Name Aliases (v10.5.0+)
可以使用方括号将别名添加到实体。如果提供,别名将显示在图中而不是实体名称。
¥An alias can be added to an entity using square brackets. If provided, the alias will be showed in the diagram instead of the entity name.
属性键和注释
¥Attribute Keys and Comments
属性还可以定义 key
或注释。键可以是 PK
、FK
或 UK
,分别表示主键、外键或唯一键。要在单个属性上指定多个键约束,请用逗号分隔它们(例如,PK, FK
)。comment
由属性末尾的双引号定义。注释本身不能包含双引号字符。
¥Attributes may also have a key
or comment defined. Keys can be PK
, FK
or UK
, for Primary Key, Foreign Key or Unique Key. To specify multiple key constraints on a single attribute, separate them with a comma (e.g., PK, FK
). A comment
is defined by double quotes at the end of an attribute. Comments themselves cannot have double-quote characters in them.
其他事情
¥Other Things
如果你希望关系标签超过一个单词,则必须在短语周围使用双引号
¥If you want the relationship label to be more than one word, you must use double quotes around the phrase
如果你根本不需要关系上的标签,则必须使用空双引号字符串
¥If you don't want a label at all on a relationship, you must use an empty double-quoted string
(v11.1.0+)如果你想要在关系上使用多行标签,请在两行之间使用
<br />
("first line<br />second line"
)¥(v11.1.0+) If you want a multi-line label on a relationship, use
<br />
between the two lines ("first line<br />second line"
)
样式
¥Styling
配置选项
¥Config options
对于简单的颜色定制:
¥For simple color customization:
名称 | 用作 |
---|---|
fill | 实体或属性的背景颜色 |
stroke | 实体或属性的边框颜色、关系的线条颜色 |
使用的类
¥Classes used
以下 CSS 类选择器可用于更丰富的样式:
¥The following CSS class selectors are available for richer styling:
选择器 | 描述 |
---|---|
.er.attributeBoxEven | 包含偶数行属性的框 |
.er.attributeBoxOdd | 包含奇数行属性的框 |
.er.entityBox | 代表实体的方框 |
.er.entityLabel | 实体的标签 |
.er.relationshipLabel | 关系的标签 |
.er.relationshipLabelBox | 关系标签周围的框 |
.er.relationshipLine | 表示实体之间关系的线 |