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.