Community程式設計與開發github.com

資料庫設計師

全面的資料庫設計工具,可建立包含資料表、欄位、索引和 ER 圖的完整資料庫結構。當使用者要求為任何系統進行資料庫設計時使用。可產生 Markdown 文件、SQL 腳本以及 DrawDB 相容的 JSON/DBML 檔案。支援 MySQL、PostgreSQL、SQL Server。觸發詞:資料庫設計、結構、ER 圖、SQL、資料模型。

資料庫設計師 是什麼?

這是一個為開發者與 AI 代理(如 Claude Code)設計的專業技能,用於自動化完整資料庫結構的設計與產出。其主要價值在於遵循一套嚴謹的設計原則(如不使用實體外鍵、強制 snake_case 命名、為所有表格與欄位添加註解等),並能根據使用者模糊的需求(例如「設計一個電商系統」)智慧推斷出核心實體與欄位。該技能支援 MySQL、PostgreSQL 和 SQL Server 等多種資料庫,並會根據目標資料庫自動調整 SQL 語法。其運作流程包括:解析需求、載入參考文件、設計表結構與索引策略、最後產生四種標準產出檔案:一份詳細的 Markdown 設計文件、可執行的 SQL 腳本、以及分別以 JSON 和 DBML 格式呈現、與 DrawDB 視覺化工具相容的檔案。所有產出檔案會依照統一的目錄結構(如 outputs/<專案名稱>/database/)進行儲存,便於後續整合與使用。

相容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/shining319/claude-code-single-person-workflow/tree/main/skills/database-designer

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

資料庫設計師 是做什麼的?

根據使用者需求,設計完整且可投入生產的資料庫結構。產生全面的文件、SQL 腳本,以及與 DrawDB 相容的視覺化 ER 圖檔案。

核心設計原則

在開始任何設計之前,務必先閱讀 references/design-principles.md,以了解:

  • 不使用實體外鍵(僅使用邏輯關聯)
  • 根據實際使用情況設定合理的欄位大小
  • 最小化且具策略性的索引設計
  • 所有資料表與欄位皆須強制加上註解
  • 預設系統欄位(id, 時間戳記, 軟刪除)
  • 使用 snake_case 命名慣例

工作流程

步驟 1:了解需求

收集關於資料庫設計的資訊:

  1. 資料庫類型:MySQL 8.0(預設)、PostgreSQL、SQL Server 等
  2. 商業領域:電子商務、部落格、CRM、ERP 等
  3. 核心實體:需要哪些主要資料表?
  4. 關鍵功能:資料庫應支援哪些功能?
  5. 特殊需求:是否有特定的限制或偏好?

如果使用者提供的資訊很少,應根據常見的業務場景和 references/design-examples.md 中記錄的最佳實踐,智慧地推斷缺失的細節。

關鍵推斷場景:

  • 使用者說「設計一個使用者資料表」→ 推斷:使用者名稱、密碼、電子郵件、電話、狀態
  • 使用者說「電子商務系統」→ 推斷:使用者、商品、訂單、訂單明細資料表
  • 使用者說「部落格系統」→ 推斷:使用者、文章、評論、標籤、文章標籤資料表

步驟 2:載入參考文件

根據設計需求,載入適當的參考資料:

  • 總是載入:references/design-principles.md 以了解核心設計規則
  • 用於格式產生:references/drawdb-formats.md 以了解 JSON/DBML 規格
  • 用於範例:references/design-examples.md 以了解類似系統的設計

步驟 3:設計資料庫結構

依照以下指南建立完整的資料表結構:

資料表設計檢查清單

針對每個資料表:

  • ✅ 新增預設系統欄位(除非使用者另有指定):

    • id BIGINT AUTO_INCREMENT PRIMARY KEY
    • created_at DATETIME DEFAULT CURRENT_TIMESTAMP
    • updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    • is_deleted TINYINT(1) DEFAULT 0
  • ✅ 使用實際的大小設計業務欄位:

    • username: VARCHAR(50)
    • email: VARCHAR(100)
    • phone: VARCHAR(20)
    • title: VARCHAR(200)
    • description: TEXT
    • 完整的尺寸標準請參閱 references/design-principles.md
  • ✅ 加入適當的限制條件:

    • 必要欄位使用 NOT NULL
    • 可選欄位使用 DEFAULT 值
    • 需要唯一性的欄位使用 UNIQUE(但不作為外鍵)
  • ✅ 為每個資料表和欄位加上 COMMENT(使用中文)

  • ✅ 不要建立實體的外鍵約束(FOREIGN KEY)

索引設計策略

針對每個資料表,分析並為以下條件建立索引:

  1. WHERE 子句欄位:經常用於篩選的欄位
  2. JOIN 欄位:所有 xxx_id 外鍵欄位
  3. ORDER BY / GROUP BY 欄位:用於排序和分組的欄位
  4. 唯一性約束:email, phone, order_no 等 → 使用 UNIQUE 索引

索引命名慣例:

  • 一般索引:idx_field_name 或 idx_field1_field2
  • 唯一索引:uk_field_name

索引限制:

  • 每個資料表最多 5 個索引(除非使用者要求更多)
  • 僅建立直接支援業務查詢的索引

關聯設計

識別資料表之間的邏輯關聯:

  1. 一對多 (1:N):最常見

    • 範例:user (1) → order (N)
    • 實作:在 order 資料表中加入 user_id
    • DrawDB:多對一關聯
  2. 一對一 (1:1):用於資料表拆分

    • 範例:user (1) → user_profile (1)
    • 實作:在 user_profile 資料表中加入 user_id UNIQUE
    • DrawDB:一對一關聯
  3. 多對多 (N:N):需要中介資料表

    • 範例:article (N) ↔ tag (N)
    • 實作:建立 article_tag 中介資料表,包含 article_id + tag_id
    • DrawDB:兩個多對一關聯

步驟 4:產生輸出

建立所有必要的輸出檔案:

4.1 全面設計文件 (Markdown)

建立一個結構良好的單一 Markdown 檔案,包含:

結構:

# [專案名稱] 資料庫設計文件

## 1. 資料庫概覽
- 資料庫類型
- 字元集
- 核心資料表數量
- 主要功能模組

## 2. 資料表結構設計

### 2.1 [資料表名稱]
**資料表名**: table_name
**說明**: 資料表說明

**欄位列表**:
| 欄位名 | 類型 | 允許空 | 預設值 | 說明 |
|--------|------|--------|--------|------|
| id | BIGINT | NO | | 主鍵 |
| ... | ... | ... | ... | ... |

**索引列表**:
| 索引名 | 類型 | 欄位 |
|--------|------|------|
| uk_email | UNIQUE | email |
| idx_username | INDEX | username |

### 2.2 [下一個資料表]
...

## 3. 資料表關聯說明
- table1 → table2 (1:N): 說明
- table3 ↔ table4 (N:N): 透過中介資料表說明

## 4. 索引策略說明
解釋索引設計決策背後的理由

4.2 SQL 腳本

建立可執行的 SQL 腳本:

-- 資料庫: project_name
-- 產生時間: YYYY-MM-DD

-- 刪除資料表(依反向依賴順序)
DROP TABLE IF EXISTS `table3`;
DROP TABLE IF EXISTS `table2`;
DROP TABLE IF EXISTS `table1`;

-- 建立資料表(依依賴順序)
CREATE TABLE `table1` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '註解',
  ...
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_field` (`field`),
  KEY `idx_field` (`field`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='資料表註解';

CREATE TABLE `table2` (
  ...
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='資料表註解';

4.3 DrawDB JSON 格式

依照 references/drawdb-formats.md 產生與 DrawDB 相容的 JSON 檔案:

關鍵格式要求:

  • 資料表 ID:使用隨機的 21 字元字串(例如 "_KV5MtPf2m4sI7Inu8pat")
  • 欄位 ID:使用隨機的 21 字元字串(例如 "B8rPRTDtOv9oD2Gp4bhWL")
  • 索引 ID:使用從 0 開始的數字遞增(跨所有資料表)
  • 關聯 ID:使用隨機的 21 字元字串
  • 欄位類型:包含完整的類型定義(例如 "VARCHAR(100)",而非分開的類型和大小)
  • 無 size 欄位:類型資訊已嵌入在類型字串中
  • 索引欄位:使用欄位名稱字串,而非欄位 ID(例如 ["email"] 而非 [1])
  • 座標:使用浮點數表示 x/y 位置

ID 產生:

import random
import string

def generate_id():
    chars = string.ascii_letters + string.digits + '_-'
    return ''.join(random.choice(chars) for _ in range(21))

佈局策略:

  • 使用網格佈局:x = (index % 3) * 450 + 50,y = floor(index / 3) * 400 + 50
  • 依序循環使用預設顏色:["#6360f7", "#bc49c4", "#ffe159", "#89e667", "#ff9159", "#59d9ff", "#ff5959", "#a0a0a0"]

關聯產生:

  • 為所有 xxx_id 外鍵欄位產生關聯
  • 使用實際的隨機字串 ID 來表示資料表和欄位
  • 根據 UNIQUE 約束設定基數:
    • 有 UNIQUE → "one_to_one"
    • 無 UNIQUE → "many_to_one"

結構:

{
  "tables": [
    {
      "id": "random_string",
      "name": "table_name",
      "comment": "表註解",
      "color": "#6360f7",
      "fields": [
        {
          "id": "random_string",
          "name": "id",
          "type": "BIGINT",
          "default": "",
          "check": "",
          "primary": true,
          "unique": true,
          "notNull": true,
          "increment": true,
          "comment": "主鍵 ID"
        }
      ],
      "indices": [
        {
          "id": 0,
          "fields": ["field_name"],
          "name": "idx_field_name",
          "unique": false
        }
      ],
      "x": 50.0,
      "y": 50.0
    }
  ],
  "relationships": [
    {
      "name": "",
      "startTableId": "random_string",
      "endTableId": "random_string",
      "endFieldId": "random_string",
      "startFieldId": "random_string",
      "id": "random_string",
      "updateConstraint": "No action",
      "deleteConstraint": "No action",
      "cardinality": "many_to_one"
    }
  ],
  "notes": [],
  "subjectAreas": [],
  "database": "generic",
  "types": [],
  "title": "Project Database"
}

4.4 DrawDB DBML 格式

依照 references/drawdb-formats.md 產生 DBML 檔案:

關鍵要點:

  • 使用小寫資料類型:bigint、varchar(50)、datetime
  • 在方括號中加入屬性:[pk, increment, not null, unique, note: '註解']
  • 在資料表定義中的 indexes { } 區塊內定義索引
  • 使用 Ref 區塊在資料表外部定義關聯
  • 使用適當的關聯符號:>(多對一)、-(一對一)

結構:

Table table_name [headercolor: #6360f7] {
  id bigint [pk, increment, not null, unique, note: '註解']
  field varchar(100) [not null, note: '註解']
  
  indexes {
    field [unique, name: 'uk_field']
  }
  
  Note: '資料表註解'
}

Ref fk_name {
  table1.field > table2.id [delete: no action, update: no action]
}

步驟 5:最終化與交付

5.1 輸出目錄慣例

建議方式(遵循 Claude Code 官方標準):

將所有資料庫設計檔案儲存至 outputs/<專案名稱>/database/:

outputs/
└── <專案名稱>/              # 專案名稱(例如 e-commerce-system)
    └── database/
        ├── schema-design.md     # 全面設計文件
        ├── schema.sql           # 可執行 SQL 腳本
        ├── drawdb-schema.json   # DrawDB JSON 格式
        └── drawdb-schema.dbml   # DrawDB DBML 格式

範例:

outputs/
├── e-commerce-system/
│   └── database/
│       ├── schema-design.md
│       ├── schema.sql
│       ├── drawdb-schema.json
│       └── drawdb-schema.dbml
└── task-management-app/
    └── database/
        ├── schema-design.md
        └── schema.sql

替代方式(傳統專案結構):

如果您的專案已有現有的目錄結構,也可以使用:

project-root/
└── database/
    ├── schema-design.md
    ├── schema.sql
    ├── drawdb-schema.json
    └── drawdb-schema.dbml

5.2 輸出檔案列表

必要輸出(4 個檔案):

  • schema-design.md - 全面的資料庫設計文件(中文)
  • schema.sql - 可執行的 SQL 腳本
  • drawdb-schema.json - DrawDB JSON 格式
  • drawdb-schema.dbml - DrawDB DBML 格式

可選輸出:

  • er-diagram.png - ER 圖視覺化(如果工具可用)
  • index-strategy.md - 索引策略文件

5.3 檔案命名慣例

  • 使用 kebab-case:user-authentication-schema.sql
  • 必要時包含版本/日期:schema-v1.0.sql 或 schema-2024-12-10.sql
  • 使用描述性名稱:e-commerce-database-schema.sql

5.4 交付摘要

在產生所有檔案後,提供一份摘要,包含:

  • 設計決策的簡要說明
  • 建立的資料表數量
  • 關鍵關聯及其理由
  • 如何匯入 DrawDB(JSON 或 DBML)
  • 檔案儲存位置的確認
  • 後續步驟建議(例如,審查索引、在開發環境中實作)

特殊處理

當使用者說「不要系統欄位」

僅建立最小結構:

CREATE TABLE `table_name` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  /* 僅業務欄位 */
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='資料表註解';

當使用者指定非 MySQL 資料庫

相應調整 SQL 語法:

PostgreSQL:

  • 使用 SERIAL 或 BIGSERIAL 替代 AUTO_INCREMENT
  • 使用 TIMESTAMP 替代 DATETIME
  • 預設值和約束的語法不同

SQL Server:

  • 使用 IDENTITY(1,1) 替代 AUTO_INCREMENT
  • 使用 DATETIME2 替代 DATETIME
  • 使用 NVARCHAR 以支援 Unicode

Oracle:

  • 使用 SEQUENCE 來實現自動遞增
  • 使用 VARCHAR2 替代 VARCHAR
  • 使用 DATE 或 TIMESTAMP 來處理時間欄位

當需求不明確時

運用智慧推斷:

  1. 識別商業領域(電子商務、部落格、CRM 等)
  2. 參考 references/design-examples.md 中的類似範例
  3. 包含該領域常見的欄位
  4. 根據典型的查詢模式設計合理的索引
  5. 在設計文件中說明所做的假設

品質驗證

在最終確定前,請驗證:

  • 所有資料表都有 COMMENT
  • 所有欄位都有 COMMENT
  • 欄位大小符合實際情況(非一般的 VARCHAR(255))
  • 每個資料表的索引數量 ≤ 5
  • 沒有實體的外鍵約束
  • 已加入系統欄位(除非使用者另有指定)
  • 所有名稱使用 snake_case
  • 邏輯關聯已清楚記錄
  • SQL 語法與目標資料庫類型相符
  • JSON/DBML 格式遵循 DrawDB 規範
  • 所有外鍵關聯都有對應的 Ref 條目

範例呼叫

使用者說:「設計一個電商系統的資料庫,包括使用者、商品、訂單功能」 → 讀取 design-principles.md 和 design-examples.md → 設計:user_info, product_info, order_info, order_detail 資料表 → 產生所有 4 個輸出檔案

使用者說:「Design a blog database」 → 推斷:user, article, comment, tag, article_tag 資料表 → 遵循標準設計原則 → 產生所有輸出

使用者說:「Create a user table for PostgreSQL」 → 單一資料表設計 → 為 PostgreSQL 調整 SQL 語法 → 包含在所有輸出格式中

參考資料

  • references/design-principles.md - 核心設計規則與標準
  • references/drawdb-formats.md - JSON 與 DBML 格式規格
  • references/design-examples.md - 真實世界的設計範例

Individual skills in this repo

This repo contains 5 individual skills — each has its own dedicated page.

academic-writing-style

Personalized academic writing assistant for university assignments in Chinese and English. Use when users need help writing/revising academic reports, project docs, technical analyses, research reviews, or case studies. Produces natural prose avoiding AI markers. Triggers: academic writing, assignment, report, technical analysis, research review, case study. | 个性化学术写作助手,适用于中英文大学作业。触发词:学术写作、作业、报告、技术分析、研究综述、案例研究、项目文档。

product-manager

Senior product manager for end-to-end product development from requirements to design. Use when users need: (1) Requirements analysis and product direction, (2) User personas and scenario analysis, (3) Feature and interaction design, (4) PRD writing, (5) Feasibility assessment. Triggers: product design, requirements, user persona, PRD, feature planning, MVP, user story. | 资深产品经理,负责需求分析到产品设计全流程。触发词:产品设计、需求分析、用户画像、PRD、功能规划、MVP、用户故事、产品方案。

solution-architect

Transform product requirements into executable technical architecture. Use when users request: (1) Technical/system architecture design, (2) Tech stack selection, (3) Architecture from PRD, (4) Deployment strategy, (5) Architecture review. Provides workflows from requirements to deployment with tech stack templates. Triggers: architecture, tech stack, system design, deployment, infrastructure. | 将产品需求转化为技术架构。触发词:架构设计、技术架构、系统架构、技术选型、部署方案、基础设施。

ui-designer

Senior UI/UX designer transforming product requirements into professional design solutions. Use when users need: (1) Page structure and information architecture, (2) UI layout and components, (3) Color schemes and visual specs, (4) Design specification docs, (5) User flow diagrams. Triggers: UI design, page design, interface design, interaction design, design specs, layout. | 资深UI/UX设计师,将需求转化为设计方案。触发词:UI设计、页面设计、界面设计、交互设计、设计规范、页面布局、设计方案。

ui-ux-pro-max

Data-driven UI/UX design intelligence with 50+ styles, 21 palettes, 50 font pairings, and 9 framework stacks (React, Next.js, Vue, Svelte, SwiftUI, React Native, Flutter, Tailwind, shadcn/ui). Searchable design knowledge base for professional UI/UX implementation. | 数据驱动的专业 UI/UX 设计智能,包含 50+ 设计风格、21 种配色方案、50 种字体配对,支持 9 种前端框架。可搜索的设计知识库,用于专业 UI/UX 实现。

相關技能