0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MermaidでER図って作れるの知ってた?

0
Last updated at Posted at 2026-03-29

ER図って普通ツールで作るイメージありますが、
Mermaidでも書けます。

こんな感じ

erDiagram
  USERS ||--o{ ORDERS : places

記号

|| → 1
o{ → 多

👉 これで1対多を表現できます

メリット

  • コードで管理できる
  • READMEにそのまま貼れる
  • 修正が楽

SQLから作るとさらに便利

例えば

-- テーブル1: users
CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(100) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE
);

-- テーブル2: orders
CREATE TABLE orders (
    order_id INT NOT NULL,
    tenant_id INT NOT NULL,
    user_id INT NOT NULL,
    PRIMARY KEY (order_id, tenant_id),
    FOREIGN KEY (user_id) REFERENCES users(id),
);

👉 ER図にすると関係が一瞬で理解できます

ER_1774808147049.png

SELECTからも作れる

例えば

SELECT *
FROM users u
JOIN orders o ON u.id = o.user_id;

ER_1774808195390.png


実際にはツールで自動生成するのが楽です
(SQL2ER - SELECT / CREATE対応)


こういう「コードで図を書く」スタイル、
地味に実務でかなり使えますよね。。。

※より詳しくはこちら

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?