SQL 转 ERD

粘贴一条或多条 CREATE TABLE 语句,本工具会绘制出模式:每张表变成一个实体,列变成标注 PK / FK 的属性,FOREIGN KEY 约束变成关系线。当模式没有声明外键时,customer_id 之类的列会作为推断关系提供,可保留或删除。可复制 Mermaid erDiagram 源码或导出为 SVG / PNG。全部本地运行——模式永不离开你的浏览器。
什么是 ERD?ERD 即实体关系图(Entity Relationship Diagram)——一幅展示数据库组织方式的图。它由三种图形构成:实体(表,画成方框)、属性(框内列出的列)与关系(连接两个方框的线)。该技术源自 Peter Chen 1976 年的实体-关系模型,至今仍是写任何 DDL 之前勾勒模式的常用方法:一张好的 ERD 一眼就能看出有哪些表、每行存什么、一张表的行如何指向另一张表的行——不懂 SQL 的人也能读懂。
Entity — one thing the system keeps data about, such as a customer, an order or a product. In a relational database each entity becomes one table.
Attribute — one fact recorded for every row of an entity, such as a name, a price or a date. Each attribute becomes one column.
Relationship — an association between two entities, implemented by a foreign key: the child table stores the parent’s key, and the diagram turns that column into a line.
PK / FK — a primary key (PK) identifies each row of a table uniquely; a foreign key (FK) holds the primary key of another table and is what a relationship line represents.
基数 — 一条连线的两端各允许多少行:一对一、一对多或多对多。本页绘制的是 Mermaid 使用的鱼尾纹表示法(参见第 4 步的图例)。
记法:Chen 记法用菱形表示关系、椭圆表示属性;鸦脚记法(本页所绘)把实体压缩为单个方框、属性列在框内,并用三叉"鸦脚"标记线的多端;UML 类图用类、操作与多重性描述同一结构。三者对模式的表达一致——只是符号不同。
1 — 表模式(DDL)
2 — 实体
绘制 表 列数 关系数 属性
尚未解析到表。
3 — 关系
使用 子表(多端) Actions 父表(一端) 来源 Actions
暂无关系。
手动添加关系:
→
4 — 图表
Mermaid erDiagram 源码
关系如何读:子表(child)上的外键产生 PARENT ||--o{ CHILD。实体与属性保留 DDL 名称,Mermaid 无法绘制的字符(空格、连字符)显示为下划线。类型简化为基础词(DECIMAL(10,2) → DECIMAL),因为图表语法不接受参数。
基数符号(鱼尾纹): 实体旁的标记表示该连线这一端允许的行数 — || 恰好一个、o| 零或一个、o{ 零或多个、|{ 一个或多个。因此 PARENT ||--o{ CHILD 读作 “一个父行可以被零个、一个或多个子行引用,而每个子行都指向唯一的一个父行” — 典型的一对多关联。
Many-to-many is never a single line: a product can appear in many orders and an order can hold many products, so the schema stores a join table (order_items) whose two foreign keys become two one-to-many lines. Read through that middle box and the many-to-many is there.