Use when you have a written implementation plan to execute in a separate session with review checkpoints
0
0
# Install this skill:
npx skills add BryantChi/webdev-skills --skill "資料庫技能"
Install specific skill from multi-skill repository
# Description
Schema 設計、ORM 和遷移指南
# SKILL.md
name: 資料庫技能
description: Schema 設計、ORM 和遷移指南
🗄️ 資料庫技能
Schema 設計原則
命名規範
| 項目 | 規範 | 範例 |
|---|---|---|
| 表名 | 複數、snake_case | users, blog_posts |
| 欄位 | 單數、snake_case | user_id, created_at |
| 主鍵 | id | id |
| 外鍵 | 表名單數_id | user_id, post_id |
常用欄位類型
-- 主鍵
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
-- 字串
name VARCHAR(255)
email VARCHAR(255) UNIQUE
content TEXT
status ENUM('draft', 'published')
-- 數字
price DECIMAL(10, 2)
quantity INT UNSIGNED
-- 時間
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
deleted_at TIMESTAMP NULL
-- 布林
is_active BOOLEAN DEFAULT TRUE
關聯類型
一對一 (1:1)
users.id → profiles.user_id
一對多 (1:N)
users.id → posts.user_id
多對多 (M:N)
posts ← post_tag → tags
索引
-- 單欄索引
CREATE INDEX idx_email ON users(email);
-- 複合索引
CREATE INDEX idx_status_created ON posts(status, created_at);
-- 唯一索引
CREATE UNIQUE INDEX idx_unique_email ON users(email);
索引原則
- 經常 WHERE 的欄位
- JOIN 的外鍵欄位
- ORDER BY 的欄位
- 避免過多索引
ORM 使用
Eloquent (Laravel)
// 關聯定義
class User extends Model {
public function posts() {
return $this->hasMany(Post::class);
}
}
// 查詢
$users = User::with('posts')
->where('status', 'active')
->orderBy('created_at', 'desc')
->paginate(20);
Prisma (Node.js)
const users = await prisma.user.findMany({
where: { status: 'active' },
include: { posts: true },
orderBy: { createdAt: 'desc' },
take: 20,
});
遷移
原則
- 遷移檔案要有版本控制
- 每次變更一個遷移
- 不要修改已執行的遷移
- 測試環境先執行
Laravel 範例
// 建立
php artisan make:migration create_posts_table
// 執行
php artisan migrate
// 回滾
php artisan migrate:rollback
# Supported AI Coding Agents
This skill is compatible with the SKILL.md standard and works with all major AI coding agents:
Amp
Antigravity
Claude Code
Clawdbot
Codex
Cursor
Droid
Gemini CLI
GitHub Copilot
Goose
Kilo Code
Kiro CLI
OpenCode
Roo Code
Trae
Windsurf
Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.