实体关系图
ER模型,全称为实体联系模型、实体关系模型或实体联系模式图(ERD)(英语:Entity-relationship model)由美籍华裔计算机科学家陈品山发明,是概念数据模型的高层描述所使用的数据模型或模式图 百度百科.
注意 ER 建模的从业者几乎总是把实体类型简单地称为实体。例如,消费者
实体类型会简单地称为消费者
实体。这种用法非常普遍,因此最好不要做任何其他事情,但从技术上讲,实体是实体类型的抽象实例,而这正是 ER 图所展示的 - 抽象实例及其之间的关系。这就是为什么实体总是使用单数名词命名的原因。
Mermaid 可以渲染ER图表
CSDN mermaid v8.14.0
不支持双引号包含 Unicode字符
、不支持 title
标签
以下是 mermaid v8.14.0
的代码
代码:
erDiagram
XiaoFeiZhe ||--o{ DingDan : "下单"
DingDan ||--|{ DingDanMingXi : "包含"
XiaoFeiZhe }|\.\.|{ ShouHuoDiZhi : "使用"
- XiaoFeiZhe: 消费者
- DingDan: 订单
- DingDanMingXi: 订单明细
- ShouHuoDiZhi: 收货地址
以下是 mermaid v10.2.3
的代码示例
---
title: 订单示例
---
erDiagram
"消费者" ||--o{ "订单" : "下单"
"订单" ||--|{ "订单明细" : "包含"
"消费者" }|\.\.|{ "收货地址" : "使用"
---
title: 订单示例
---
erDiagram
"消费者" ||--o{ "订单" : "下单"
"订单" ||--|{ "订单明细" : "包含"
"消费者" }|..|{ "收货地址" : "使用"
实体名称通常首字母大写,虽然没有一种公认的标准来规定这一点,在 Mermaid 中也不是必须的。
实体之间的关系由具有基数表示的终端标记的线表示。Mermaid 使用最流行的鸟脚符号表示法。鸟脚符号直观地传达了与其连接的实体的实例数量的可能性。
ER 图可以用于各种各样的目的,从不包含任何实现细节的抽象逻辑模型到关系型数据库表的物理模型。在 ER 图中包含属性定义可以有助于理解实体的目的和意义。这些属性定义不一定需要详尽无遗;通常一小部分属性就足够了。Mermaid 允许以其类型和名称来定义这些属性。
代码:
***CSDN mermaid v8.14.0
***
- 不支持双引号包含
Unicode字符
定义变量
以下是 mermaid v8.14.0
的代码示例
erDiagram
XiaoFeiZhe ||--o{ DingDan : "下单"
XiaoFeiZhe {
string XiaoFeiZheXingMing
string XiaoFeiZheXuHao
string XiaoFeiZheHangYe
}
DingDan ||--|{ DingDanMingXi : "包含"
DingDan {
int DingDanXuHao
string ShouHuoDiZhi
}
DingDanMingXi {
string ChanPinXuHao
int ChanPinShuLiang
float ChanPinDanJia
}
- XiaoFeiZhe: 消费者
- DingDan: 订单
- DingDanMingXi: 订单明细
- ShouHuoDiZhi: 收货地址
- XiaoFeiZheXingMing: 消费者姓名
- XiaoFeiZheXuHao: 消费者序号
- XiaoFeiZheHangYe: 消费者行业
- DingDanXuHao: 订单序号
- ShouHuoDiZhi: 收货地址
- ChanPinXuHao: 产品序号
- ChanPinShuLiang: 产品数量
- ChanPinDanJia: 产品单价
以下是 mermaid v10.2.3
的代码示例
erDiagram
"消费者" ||--o{ "订单" : "下单"
"消费者" {
string XiaoFeiZheXingMing
string XiaoFeiZheXuHao
string XiaoFeiZheHangYe
}
"订单" ||--|{ "订单明细" : "包含"
"订单" {
int DingDanXuHao
string ShouHuoDiZhi
}
"订单明细" {
string ChanPinXuHao
int ChanPinShuLiang
float ChanPinDanJia
}
在 ER 图中包含属性时,您必须决定是否将外键作为属性包含。这可能取决于您需要多么准确地表示关系表结构。如果您的图是一个逻辑模型而不是关系模型,那么最好将其省略,因为关联关系已经传达了实体如何彼此关联的方式。例如,JSON 数据结构可以使用数组实现一对多的关系,而不需要外键属性。同样,面向对象编程语言可以使用指针或引用集合。即使是为关系实现而设计的模型,您也可以决定包含外键属性会重复已由关系描绘的信息,并且不会为实体添加含义。
语法
实体和关系
Mermaid ER图的语法与 PlantUML 兼容,可以通过扩展标签关系来实现。每个语句由以下部分组成:
<第一个实体> [<关系> <第二个实体> : <关系标签>]
其中:
-
第一个实体
是实体名称。名称必须以字母开头,可以包含数字、连字符和下划线。 -
关系
描述了两个实体之间的相互关系。请参见以下说明。 -
第二个实体
是另一个实体名称 -
关系标签
描述了以第一个实体为视角的关系。
例如:“别墅” ||–|{ “房间” : “包含”
这个语句可以被理解为一个别墅包含一个或多个房间,同时房间只是一个别墅的一部分。你可以看到这个标签是以第一个实体的视角来描述的:一个别墅包括一个房间,但一个房间并不包括一个别墅。当从第二个实体的视角考虑时,等效的标签通常很容易推断出来。(有些ER图会从两个实体的角度标注关系,但在这里不支持这种方式,而且通常是多余的)。
语句中只有第一个实体的部分是必须的。这使得在 ER 图的迭代构建过程中可以显示没有关系的实体,这在某些情况下很有用。如果指定了语句的任何其他部分,则所有部分都是必需的。
关系语法
每个语句中的关系部分可以被分解为三个子组件:
- 相对于第二个实体的基数。
- 关系是否授予“子”实体身份。
- 相对于第一个实体的基数。
基数是描述另一个实体中可以与其所关联的实体相关联的元素数量的属性。 在上面的例子中,一个别墅
可以和一个或多个房间
实例相关联,而一个房间
只能与一个别墅
相关联。在每个基数标记中都有两个字符。最外层的字符表示最大值,最内层的字符表示最小值。下表总结了可能出现的基数。
左侧值 | 右侧值 | 含义 |
---|---|---|
|o | o| | 0或1 |
|| | || | 恰好一个 |
}o | o{ | 0 或 更多 (没有上限) |
}| | |{ | 1 或 更多 (没有上限) |
别名 |
左侧值 | 右侧值 | Alias for |
---|---|---|
one or zero | one or zero | 0或1 |
zero or one | zero or one | 0或1 |
one or more | one or more | 1 或 更多 |
one or many | one or many | 1 或 更多 |
many(1) | many(1) | 1 或 更多 |
1+ | 1+ | 1 或 更多 |
zero or more | zero or more | 0 或 更多 |
zero or many | zero or many | 0 或 更多 |
many(0) | many(1) | 0 或 更多 |
0+ | 0+ | 0 或 更多 |
only one | only one | 恰好一个 |
1 | 1 | 恰好一个 |
标识
关系可以分为标识和非标识,分别用实线或虚线表示。这在涉及到一个实体没有独立存在的情况下是很重要的。例如,一家保险公司可能需要储存关于 驾驶员
的数据。在建模时,我们可能会观察到 汽车
可以被许多 人
驾驶,而 人
可以驾驶许多 汽车
- 这两个实体都可以独立存在,因此这是一种非标识关系,我们可以在 Mermaid
中指定为:"人" }|..|{ "汽车":"驾驶"
。请注意,关系中间有两个点,这将导致两个实体之间绘制出一个虚线。但是当这个多对多关系被拆分成两个一对多关系时,我们会发现 驾驶员
不能没有 人
和 汽车
存在——这些关系变成了标识性关系,会使用连字符进行指定,其对应实线:
别名
值 | 称作 |
---|---|
to | 标识 |
optionally to | 不可标识 |
代码:
***CSDN mermaid v8.14.0
***
- 不支持双引号包含
Unicode字符
定义变量
以下是 mermaid v8.14.0
的代码示例
erDiagram
QiChe ||--o{ JiaShiYuan : "允许"
Ren ||--o{ JiaShiYuan : "是"
- QiChe: 汽车
- Ren: 人
- JiaShiYuan: 驾驶员
以下是 mermaid v10.2.3
的代码示例
erDiagram
"汽车" ||--o{ "驾驶员" : "允许"
"人" ||--o{ "驾驶员" : "是"
属性
可以通过指定实体名称,后跟具有多个类型名称对的一个块来为实体定义属性,其中一个块由一个开放的 {
和一个关闭的 }
来界定。属性被呈现在实体框内。例如:
代码:
***CSDN mermaid v8.14.0
***
- 不支持双引号包含
Unicode字符
定义变量
以下是 mermaid v8.14.0
的代码示例
erDiagram
QiChe ||--o{ JiaShiYuan : "允许"
QiChe {
string ZhuCeHao
string ZhiZaoShang
string XingHao
}
Ren ||--o{ JiaShiYuan : "是"
Ren {
string XingShi
string MingZi
int NianLing
}
- QiChe: 汽车
- ZhuCeHao: 注册号
- ZhiZaoShang: 制造商
- XingHao: 型号
- Ren: 人
- XingShi: 姓氏
- MingZi: 名字
- NianLing: 年龄
- JiaShiYuan: 驾驶员
以下是 mermaid v10.2.3
的代码示例
erDiagram
"汽车" ||--o{ "驾驶员" : "允许"
"汽车" {
string ZhuCeHao
string ZhiZaoShang
string XingHao
}
"人" ||--o{ "驾驶员" : "是"
"人" {
string XingShi
string MingZi
int NianLing
}
类型和名称的值必须以字母字符开头,可以包含数字、连字符、下划线、括号和方括号。除此之外,没有任何限制,也没有隐含的有效数据类型集。
属性键和注释
实体属性还可以定义关键字或注释。关键字可以是PK
(Primary Key)、FK
(Foreign Key)或UK
(Unique Key),分别表示主键、外键或唯一键。要在单个属性上指定多个关键字约束,请使用逗号(例如,PK,FK
)进行分隔。注释由属性末尾的双引号定义。注释本身不能有双引号字符。
***CSDN mermaid v8.14.0
***
- 不支持双引号包含
Unicode字符
定义变量 - 不支持
string[]
定义数组 - 不支持
string(99) 定义数组长度
, - 不支持 UK定义唯一键
- 不支持 FK定义外键
- 不支持
only one to zero or more
语句来描述实体关
以下是 mermaid v8.14.0
的代码示例
代码:
erDiagram
QiChe ||--o{ JiaShiYuan : "允许"
QiChe {
string ZhuCeHao PK
string ZhiZaoShang
string XingHao
string LingBuJian
}
Ren ||--o{ JiaShiYuan : "是"
Ren {
string JiaShiZheng PK "驾驶证 #"
string XingShi "无长度说明"
string MingZi
string ShouJiHaoMa
int NianLing
}
JiaShiYuan {
string XingShiZheng PK
string JiaShiZheng PK
}
ZhiZaoShang ||--o{ QiChe : "制造"
- QiChe: 汽车
- ZhuCeHao: 注册号
- ZhiZaoShang: 制造商
- XingHao: 型号
- LingBuJian: 零部件
- Ren: 人
- JiaShiZheng: 驾驶证
- XingShi: 姓氏
- MingZi: 名字
- ShouJiHaoMa:手机号码
- NianLing: 年龄
- JiaShiYuan: 驾驶员
- XingShiZheng: 行驶证
- JiaShiZheng: 驾驶证
- ZhiZaoShang: 制造商
以下是 mermaid v10.2.3
的代码示例
erDiagram
"汽车" ||--o{ "驾驶员" : "允许"
"汽车" {
string ZhuCeHao PK
string ZhiZaoShang
string XingHao
string[] LingBuJian
}
"人" ||--o{ "驾驶员" : "是"
"人" {
string JiaShiZheng PK "驾驶证 #"
string(99) XingShi "只允许输入99个字符"
string MingZi
string ShouJiHaoMa UK
int NianLing
}
"驾驶员" {
string XingShiZheng PK, FK
string JiaShiZheng PK, FK
}
"制造商" only one to zero or more "汽车" : "制造"
其他事项
如果你希望关系标签是多个单词,你必须在短语周围使用双引号。
如果你不想在关系上显示标签,你必须使用一个空的双引号字符串。
样式
配置选项
对于简单的颜色自定义:
名称 | 作用 |
---|---|
fill | 视图或属性的背景颜色 |
stroke | 实体或属性的边框颜色,关系的线条颜色 |
使用的类自定义样式
以下CSS类选择器可用于更丰富的样式:
选择器 | 描述 |
---|---|
.er.attributeBoxEven | 表示包含偶数行属性框 |
.er.attributeBoxOdd | 表示包含奇数行属性框 |
.er.entityBox | 表示实体框 |
.er.entityLabel | 表示实体标签文本 |
.er.relationshipLabel | 表示关系标签文本 |
.er.relationshipLabelBox | 表示关系标签框 |
.er.relationshipLine | 表示关系线 |
偶数行属性框
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mermaid 实体关系图</title>
<script src="./mermaid.min.10.2.3.js"></script>
</head>
<body>
<!--%%{init: { 'logLevel': 'debug', 'theme': 'base', 'timeline': {'disableMulticolor': true}}}%%-->
<pre class="mermaid">
erDiagram
"汽车" ||--o{ "驾驶员" : "允许"
"汽车" {
string ZhuCeHao PK
string ZhiZaoShang
string XingHao
string[] LingBuJian
}
"人" ||--o{ "驾驶员" : "是"
"人" {
string JiaShiZheng PK "驾驶证 #"
string(99) XingShi "只允许输入99个字符"
string MingZi
string ShouJiHaoMa UK
int NianLing
}
"驾驶员" {
string XingShiZheng PK, FK
string JiaShiZheng PK, FK
}
"制造商" only one to zero or more "汽车" : "制造"
</pre>
<script>
var config = {
startOnLoad:true,
logLevel: 'debug',
theme: 'base',
er:{
// titleTopMargin: 3,
// diagramPadding: 200,
// layoutDirection: "LR",
// minEntityWidth: 200,
// minEntityHeight: 20,
// entityPadding: 3,
// stroke: "red",
// fill:"blue",
// fontSize: 24,
// useMaxWidth: true
},
themeCSS: ".er.attributeBoxEven{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.attributeBoxOdd{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;width:300px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.entityLabel { fill: red; font-size:32.2px;font-family:Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLabelBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.relationshipLabel { fill: red; font-size:32.2px;font-family:楷体,Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLine { stroke:red;stroke-width:1;}"
};
mermaid.initialize(config);
</script>
</body>
</html>
奇数行属性框
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mermaid 实体关系图</title>
<script src="./mermaid.min.10.2.3.js"></script>
</head>
<body>
<!--%%{init: { 'logLevel': 'debug', 'theme': 'base', 'timeline': {'disableMulticolor': true}}}%%-->
<pre class="mermaid">
erDiagram
"汽车" ||--o{ "驾驶员" : "允许"
"汽车" {
string ZhuCeHao PK
string ZhiZaoShang
string XingHao
string[] LingBuJian
}
"人" ||--o{ "驾驶员" : "是"
"人" {
string JiaShiZheng PK "驾驶证 #"
string(99) XingShi "只允许输入99个字符"
string MingZi
string ShouJiHaoMa UK
int NianLing
}
"驾驶员" {
string XingShiZheng PK, FK
string JiaShiZheng PK, FK
}
"制造商" only one to zero or more "汽车" : "制造"
</pre>
<script>
var config = {
startOnLoad:true,
logLevel: 'debug',
theme: 'base',
er:{
// titleTopMargin: 3,
// diagramPadding: 200,
// layoutDirection: "LR",
// minEntityWidth: 200,
// minEntityHeight: 20,
// entityPadding: 3,
// stroke: "red",
// fill:"blue",
// fontSize: 24,
// useMaxWidth: true
},
//themeCSS: ".er.attributeBoxEven{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
themeCSS: ".er.attributeBoxOdd{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;width:300px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.entityLabel { fill: red; font-size:32.2px;font-family:Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLabelBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.relationshipLabel { fill: red; font-size:32.2px;font-family:楷体,Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLine { stroke:red;stroke-width:1;}"
};
mermaid.initialize(config);
</script>
</body>
</html>
实体框
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mermaid 实体关系图</title>
<script src="./mermaid.min.10.2.3.js"></script>
</head>
<body>
<!--%%{init: { 'logLevel': 'debug', 'theme': 'base', 'timeline': {'disableMulticolor': true}}}%%-->
<pre class="mermaid">
erDiagram
"汽车" ||--o{ "驾驶员" : "允许"
"汽车" {
string ZhuCeHao PK
string ZhiZaoShang
string XingHao
string[] LingBuJian
}
"人" ||--o{ "驾驶员" : "是"
"人" {
string JiaShiZheng PK "驾驶证 #"
string(99) XingShi "只允许输入99个字符"
string MingZi
string ShouJiHaoMa UK
int NianLing
}
"驾驶员" {
string XingShiZheng PK, FK
string JiaShiZheng PK, FK
}
"制造商" only one to zero or more "汽车" : "制造"
</pre>
<script>
var config = {
startOnLoad:true,
logLevel: 'debug',
theme: 'base',
er:{
// titleTopMargin: 3,
// diagramPadding: 200,
// layoutDirection: "LR",
// minEntityWidth: 200,
// minEntityHeight: 20,
// entityPadding: 3,
// stroke: "red",
// fill:"blue",
// fontSize: 24,
// useMaxWidth: true
},
//themeCSS: ".er.attributeBoxEven{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.attributeBoxOdd{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;width:300px;height:20px;}"
themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.entityLabel { fill: red; font-size:32.2px;font-family:Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLabelBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.relationshipLabel { fill: red; font-size:32.2px;font-family:楷体,Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLine { stroke:red;stroke-width:1;}"
};
mermaid.initialize(config);
</script>
</body>
</html>
实体标签文本
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mermaid 实体关系图</title>
<script src="./mermaid.min.10.2.3.js"></script>
</head>
<body>
<!--%%{init: { 'logLevel': 'debug', 'theme': 'base', 'timeline': {'disableMulticolor': true}}}%%-->
<pre class="mermaid">
erDiagram
"汽车" ||--o{ "驾驶员" : "允许"
"汽车" {
string ZhuCeHao PK
string ZhiZaoShang
string XingHao
string[] LingBuJian
}
"人" ||--o{ "驾驶员" : "是"
"人" {
string JiaShiZheng PK "驾驶证 #"
string(99) XingShi "只允许输入99个字符"
string MingZi
string ShouJiHaoMa UK
int NianLing
}
"驾驶员" {
string XingShiZheng PK, FK
string JiaShiZheng PK, FK
}
"制造商" only one to zero or more "汽车" : "制造"
</pre>
<script>
var config = {
startOnLoad:true,
logLevel: 'debug',
theme: 'base',
er:{
// titleTopMargin: 3,
// diagramPadding: 200,
// layoutDirection: "LR",
// minEntityWidth: 200,
// minEntityHeight: 20,
// entityPadding: 3,
// stroke: "red",
// fill:"blue",
// fontSize: 24,
// useMaxWidth: true
},
//themeCSS: ".er.attributeBoxEven{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.attributeBoxOdd{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;width:300px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
themeCSS: ".er.entityLabel { fill: red; font-size:32.2px;font-family:Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLabelBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.relationshipLabel { fill: red; font-size:32.2px;font-family:楷体,Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLine { stroke:red;stroke-width:1;}"
};
mermaid.initialize(config);
</script>
</body>
</html>
关系标签框
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mermaid 实体关系图</title>
<script src="./mermaid.min.10.2.3.js"></script>
</head>
<body>
<!--%%{init: { 'logLevel': 'debug', 'theme': 'base', 'timeline': {'disableMulticolor': true}}}%%-->
<pre class="mermaid">
erDiagram
"汽车" ||--o{ "驾驶员" : "允许"
"汽车" {
string ZhuCeHao PK
string ZhiZaoShang
string XingHao
string[] LingBuJian
}
"人" ||--o{ "驾驶员" : "是"
"人" {
string JiaShiZheng PK "驾驶证 #"
string(99) XingShi "只允许输入99个字符"
string MingZi
string ShouJiHaoMa UK
int NianLing
}
"驾驶员" {
string XingShiZheng PK, FK
string JiaShiZheng PK, FK
}
"制造商" only one to zero or more "汽车" : "制造"
</pre>
<script>
var config = {
startOnLoad:true,
logLevel: 'debug',
theme: 'base',
er:{
// titleTopMargin: 3,
// diagramPadding: 200,
// layoutDirection: "LR",
// minEntityWidth: 200,
// minEntityHeight: 20,
// entityPadding: 3,
// stroke: "red",
// fill:"blue",
// fontSize: 24,
// useMaxWidth: true
},
//themeCSS: ".er.attributeBoxEven{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.attributeBoxOdd{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;width:300px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.entityLabel { fill: red; font-size:32.2px;font-family:Times New Roman;font-weight:700}"
themeCSS: ".er.relationshipLabelBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.relationshipLabel { fill: red; font-size:32.2px;font-family:楷体,Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLine { stroke:red;stroke-width:1;}"
};
mermaid.initialize(config);
</script>
</body>
</html>
关系标签文本
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mermaid 实体关系图</title>
<script src="./mermaid.min.10.2.3.js"></script>
</head>
<body>
<!--%%{init: { 'logLevel': 'debug', 'theme': 'base', 'timeline': {'disableMulticolor': true}}}%%-->
<pre class="mermaid">
erDiagram
"汽车" ||--o{ "驾驶员" : "允许"
"汽车" {
string ZhuCeHao PK
string ZhiZaoShang
string XingHao
string[] LingBuJian
}
"人" ||--o{ "驾驶员" : "是"
"人" {
string JiaShiZheng PK "驾驶证 #"
string(99) XingShi "只允许输入99个字符"
string MingZi
string ShouJiHaoMa UK
int NianLing
}
"驾驶员" {
string XingShiZheng PK, FK
string JiaShiZheng PK, FK
}
"制造商" only one to zero or more "汽车" : "制造"
</pre>
<script>
var config = {
startOnLoad:true,
logLevel: 'debug',
theme: 'base',
er:{
// titleTopMargin: 3,
// diagramPadding: 200,
// layoutDirection: "LR",
// minEntityWidth: 200,
// minEntityHeight: 20,
// entityPadding: 3,
// stroke: "red",
// fill:"blue",
// fontSize: 24,
// useMaxWidth: true
},
//themeCSS: ".er.attributeBoxEven{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.attributeBoxOdd{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;width:300px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.entityLabel { fill: red; font-size:32.2px;font-family:Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLabelBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
themeCSS: ".er.relationshipLabel { fill: red; font-size:32.2px;font-family:楷体,Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLine { stroke:red;stroke-width:1;}"
};
mermaid.initialize(config);
</script>
</body>
</html>
关系线
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mermaid 实体关系图</title>
<script src="./mermaid.min.10.2.3.js"></script>
</head>
<body>
<!--%%{init: { 'logLevel': 'debug', 'theme': 'base', 'timeline': {'disableMulticolor': true}}}%%-->
<pre class="mermaid">
erDiagram
"汽车" ||--o{ "驾驶员" : "允许"
"汽车" {
string ZhuCeHao PK
string ZhiZaoShang
string XingHao
string[] LingBuJian
}
"人" ||--o{ "驾驶员" : "是"
"人" {
string JiaShiZheng PK "驾驶证 #"
string(99) XingShi "只允许输入99个字符"
string MingZi
string ShouJiHaoMa UK
int NianLing
}
"驾驶员" {
string XingShiZheng PK, FK
string JiaShiZheng PK, FK
}
"制造商" only one to zero or more "汽车" : "制造"
</pre>
<script>
var config = {
startOnLoad:true,
logLevel: 'debug',
theme: 'base',
er:{
// titleTopMargin: 3,
// diagramPadding: 200,
// layoutDirection: "LR",
// minEntityWidth: 200,
// minEntityHeight: 20,
// entityPadding: 3,
stroke: "red",
// fill:"blue",
// fontSize: 24,
// useMaxWidth: true
},
//themeCSS: ".er.attributeBoxEven{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.attributeBoxOdd{stroke:red;stroke-width:3;fill:blue;width:100px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;width:300px;height:20px;}"
//themeCSS: ".er.entityBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.entityLabel { fill: red; font-size:32.2px;font-family:Times New Roman;font-weight:700}"
//themeCSS: ".er.relationshipLabelBox{stroke:red;stroke-width:3;fill:blue;margin:6px}"
//themeCSS: ".er.relationshipLabel { fill: red; font-size:32.2px;font-family:楷体,Times New Roman;font-weight:700}"
themeCSS: ".er.relationshipLine { stroke:red;stroke-width:2;}"
};
mermaid.initialize(config);
</script>
</body>
</html>
其他样式
init.er
配置
<script>
var config = {
startOnLoad:true,
logLevel: 'debug',
er:{
titleTopMargin: 3,
diagramPadding: 200,
layoutDirection: "LR",
minEntityWidth: 200,
minEntityHeight: 20,
entityPadding: 3,
stroke: "red",
fill:"blue",
fontSize: 12,
useMaxWidth: true
}
};
mermaid.initialize(config);
</script>
- titleTopMargin :
- diagramPadding: 整个实体关系图与上下左右的距离
- layoutDirection: 实体关系图方向,支持:“TB”, “BT”, “LR”, “RL”
- minEntityWidth: 最小实体框宽度
- minEntityHeight: 最小实体框高度
- entityPadding
- stroke: 关系线的颜色
- fill:实体框的填充颜色,无效
- fontSize:字体大小
- useMaxWidth:当设置该标志为 true 时,图表宽度将被锁定为100%并根据可用空间进行缩放。如果设置为 false,则图表将保留其绝对宽度。