ERD(實體關係圖)

關於 ERD

**實體關係圖(ERD)**記錄關聯式資料庫的結構:資料表(實體)、其欄位(屬性),以及資料表之間的外鍵關係——包括基數(1..1 / 0..1 / 1..N / 0..N)。後端工程師用它向新團隊成員說明程式碼庫。資料架構師在撰寫任何 DDL 之前先在白板上畫出草圖。資料庫教育者(Elmasri & Navathe;Silberschatz / Korth / Sudarshan)將其置於每門電腦科學資料庫課程的概念設計單元核心。

Schematex 實作鳥足記號法——MySQL Workbench、dbdiagram.io、ERDPlus、Lucidchart、draw.io、Mermaid erDiagram 和 Oracle SQL Developer Data Modeler 都採用的現代實際標準。DSL 與 DBML 大致相容,讓工程師可以直接貼上;它也完整接受 Mermaid erDiagram 方言——以 erDiagram 標頭開頭,直接貼入 Mermaid 關係式(CUSTOMER ||--o{ ORDER : places)和以類型為首的實體區塊(ORDER { int id PK }),實體會從關係中自動建立。Chen 1976 記號法(矩形 + 菱形 + 橢圓形 + ISA 階層)和 Barker 覆疊層延後至 v0.2;目前每張圖都渲染為鳥足記號法。

注意 — 這與 entity 引擎不同entity(法律實體結構)用於公司/法律持有階層(子公司、股比彙總、信託)。erd 用於資料庫綱要(資料表、欄位、外鍵)。不同領域、不同佈局、不同受眾。

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·0.8 ms·8.1 KB SVG

1. 您的第一張 ERD

最精簡的實用 ERD:一張父資料表被子資料表透過外鍵引用。

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.4 ms·4.3 KB SVG

四條規則涵蓋 80% 的用法:

  1. erd 開頭。可選的標頭:title:direction: LR | TB
  2. 每張資料表是一個區塊:table 名稱 { 欄位 類型 標記 }。標記為 PKFKUKNN(或 *,表示 NOT NULL)。
  3. 行內外鍵目標:在欄位後加上 FK -> 其他表.欄位。渲染器會自動加上 FK 標籤。
  4. ref 行連接資料表:ref 來源 <左基數> -- <右基數> 目標 [: "標籤"]。基數可用具名形式(one-mandatoryone-optionalmany-mandatorymany-optional)、Min-Max 形式(1..1 / 0..1 / 1..N / 0..N),或 Mermaid 符號(}o--|| 等)作為別名。

注釋使用 //#。區塊形式可以是多行(每欄位一行)或行內(table A { id int PK; name varchar })。


2. 基數符號

鳥足記號法在關係線的每一端編碼基數:

符號讀法Min..Max具名標記
─┃(垂直線)恰好一個(強制一)1..1one-mandatory
─○(空心圓)零或一個(可選一)0..1one-optional
─┃<(線 + 鳥足)一個或多個(強制多)1..Nmany-mandatory
─○<(圓 + 鳥足)零個或多個(可選多)0..Nmany-optional

線條的每一端可獨立標注。典型的 1:N 關係讀作「恰好一個 CUSTOMER 下零個或多個 ORDER」:

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

從右到左讀:線條的右端以單線連接 Customer(one-mandatory);左端以線+鳥足連接 Order(many-mandatory)。


3. Mermaid 別名

若您已使用 Mermaid erDiagram,相同的符號可直接作為輸入:

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·0.5 ms·5.8 KB SVG
Mermaid 標記Schematex 含義
|o 左 / o|0..1
||1..1
}o 左 / o{0..N
}| 左 / |{1..N
--識別性/實線
..非識別性/虛線

4. 識別性與非識別性關係

使用 -- 表示識別性關係(實線),.. 表示非識別性關係(虛線):

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·0.5 ms·4.4 KB SVG

虛線遵循 Barker / IDEF1X 非識別性慣例。


5. 複合主鍵(關聯資料表)

關聯(連接)資料表用於解析 M:N 關係。同時帶有 PKFK 標記的欄位:

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·0.5 ms·5.7 KB SVG

Schematex 在同一列渲染兩個標籤(PK + FK)。當任何 PK 標記存在時,欄位名稱會加上底線。


6. 佈局方向

direction: LR(預設)將父資料表置於左側,子資料表置於右側。direction: TB 切換為由上到下——適合窄版嵌入:

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·0.5 ms·6.7 KB SVG

採用單折邊的分層正交曼哈頓路由。v0.1 在層內使用宣告順序堆疊;重心交叉最小化延後至 v0.2。


7. 記號模式

DSL 標頭可以固定記號法:

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

notation: chen(矩形-菱形-橢圓形、弱實體、三元關係、ISA)和 notation: barker(每半側虛線)已在 27-ERD-STANDARD.md 中記錄,但目前解析器拒絕接受。目標版本 v0.2。


8. v0.1 的限制

  • 僅支援鳥足記號法。 Chen 和 Barker 渲染延後實作。
  • 無邊線交叉最小化。 分層佈局在層內使用宣告順序;大型 ERD(10 張以上資料表、密集外鍵)會出現一些交叉。若特定佈局很重要,請重新排列 table 區塊的順序。
  • 無自我引用的 C 形曲線。 遞迴關係(Employee.manager_id -> Employee.id)可解析但路由為直正交線,而非標準的 C 形。
  • 無 M:N 自動解析。 請明確表示關聯實體(這也是生產綱要中的標準實務)。

完整標準參考請見 docs/reference/27-ERD-STANDARD.md


相關範例

範例庫中的即用情境:

erd·§ Crow's-foot (Everest 1976) / DBML compatible
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
University schema (academic ERD)
Classic textbook schema with Student / Course / Enrollment associative entity and labelled relationships — the canonical Elmasri & Navathe ch.3 example.
education
erd·§ Crow's-foot (Everest 1976) / DBML compatible
E-commerce Schema Entity-Relationship Diagram with 6 entities and 5 relationships. E-commerce Schema places Customer customer_id int PK email varchar UK name varchar created_at timestamp Category category_id int PK name varchar Address address_id int PK customer_id int FK line1 varchar city varchar zip varchar Order order_id int PK customer_id int FK placed_at timestamp status varchar Product product_id int PK category_id int FK name varchar price decimal OrderLine order_id int PK FK line_no int PK product_id int FK qty int price decimal
E-commerce schema
Production-style schema with Customer / Order / OrderLine / Product / Category — typical mid-complexity backend ERD with composite PK on the line-item table.
software & it

Found this useful?

Schematex is free, fully open source, and zero-dependency. A star helps other developers discover it.