database-schema-designer by onewave-ai/claude-skills
npx skills add https://github.com/onewave-ai/claude-skills --skill database-schema-designer设计经过优化、可扩展的数据库模式,包含适当的关系和索引。
当用户需要数据库模式设计时:
* 使用什么类型的数据库(PostgreSQL、MySQL、MongoDB 等)?
* 应用领域是什么?
* 主要的实体/资源有哪些?
* 哪些查询将最为常见?
* 预期的数据量和增长情况?
* 性能要求?
* 特定的约束或合规性需求?
2. 遵循最佳实践设计模式 :
针对 SQL 数据库 :
* 识别实体及其属性
* 定义主键(分布式系统建议使用 UUID)
* 建立关系(1:1, 1:N, N:M)
* 规范化至第三范式(除非出于性能考虑需要反规范化)
* 添加适当的索引
* 定义外键约束
* 包含时间戳(created_at, updated_at)
* 如需,添加软删除标志
* 规划数据归档
针对 NoSQL 数据库 :
* 为访问模式设计(查询优先方法)
* 决定嵌入还是引用
* 规划反规范化
* 为常见查询设计索引
* 考虑文档大小限制
* 规划最终一致性
3. 生成完整模式 :
:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
-- [实体名称] 表
-- 用途:[描述]
CREATE TABLE [table_name] (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
[field_name] [TYPE] [CONSTRAINTS],
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMP
);
-- 索引
CREATE INDEX idx_[table]_[field] ON [table]([field]);
CREATE INDEX idx_[table]_[field1]_[field2] ON [table]([field1], [field2]);
-- 外键
ALTER TABLE [child_table]
ADD CONSTRAINT fk_[constraint_name]
FOREIGN KEY ([foreign_key_field])
REFERENCES [parent_table](id)
ON DELETE CASCADE;
NoSQL 模式输出 (MongoDB 示例):
// [集合名称]
// 用途:[描述]
{
_id: ObjectId,
[field_name]: [type],
// 内嵌文档
[embedded_object]: {
field1: type,
field2: type
},
// 引用
[related_id]: ObjectId, // 引用至 [other_collection]
created_at: ISODate,
updated_at: ISODate
}
// 索引
db.[collection].createIndex({ field: 1 })
db.[collection].createIndex({ field1: 1, field2: -1 })
db.[collection].createIndex({ field: "text" }) // 文本搜索
4. 创建实体关系图 (文本格式):
┌─────────────────────┐
│ users │
├─────────────────────┤
│ id (PK) │
│ email (UNIQUE) │
│ name │
│ created_at │
└──────────┬──────────┘
│
│ 1:N
│
┌──────────▼──────────┐
│ posts │
├─────────────────────┤
│ id (PK) │
│ user_id (FK) │
│ title │
│ content │
│ created_at │
└──────────┬──────────┘
│
│ N:M (via post_tags)
│
┌──────────▼──────────┐
│ tags │
├─────────────────────┤
│ id (PK) │
│ name (UNIQUE) │
└─────────────────────┘
5. 提供迁移脚本 :
-- 迁移:create_users_table
-- 日期:2024-01-15
BEGIN;
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_created_at ON users(created_at);
COMMIT;
-- 回滚
BEGIN;
DROP TABLE users;
COMMIT;
6. 格式化完整输出 :
🗄️ 数据库模式设计
数据库:[PostgreSQL/MySQL/MongoDB/等]
领域:[应用类型]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📋 实体关系图
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[ASCII ERD]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 表定义
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[SQL CREATE TABLE 语句]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔗 关系
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[外键约束]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚡ 索引
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[索引定义及原理说明]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔄 迁移脚本
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[升级和降级迁移]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💡 优化说明
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
性能考虑:
• [索引策略]
• [分区建议]
• [反规范化机会]
扩展策略:
• [分片方法]
• [读副本]
• [缓存层]
数据完整性:
• [约束策略]
• [验证规则]
• [审计日志]
7. 模式设计最佳实践 :
命名约定 :
* 表和列名使用 snake_case
* 表名使用复数形式(users, posts)
* 使用描述性的外键名称(user_id,而不是 uid)
* 索引加前缀(idx_table_column)
* 约束加前缀(fk_, uk_, ck_)
数据类型 :
* 使用合适的类型(INT 与 BIGINT,VARCHAR 与 TEXT)
* 考虑存储大小
* 固定值集合使用 ENUM
* 灵活属性使用 JSON/JSONB
* 使用正确的日期/时间类型(TIMESTAMP 与 DATETIME)
索引 :
* 为外键建立索引
* 为 WHERE 子句中的列建立索引
* 多列查询使用复合索引
* 考虑覆盖索引
* 监控索引使用情况并移除未使用的索引
关系 :
* 在关系型数据库中始终使用外键
* 在适当的地方级联删除
* 为审计追踪考虑软删除
* 多对多关系使用连接表
性能 :
* 为读密集型工作负载反规范化
* 对大表进行分区
* 复杂查询使用物化视图
* 考虑读副本
* 规划旧数据的归档
确保模式:
生成可用于生产、经过优化且可扩展的数据库模式。
每周安装数
69
代码仓库
GitHub 星标数
75
首次出现
2026年1月24日
安全审计
安装于
opencode57
codex56
cursor56
gemini-cli55
claude-code55
github-copilot53
Design optimized, scalable database schemas with proper relationships and indexes.
When a user needs database schema design:
Gather Requirements :
Design Schema Following Best Practices :
For SQL Databases :
* Identify entities and their attributes
* Define primary keys (prefer UUIDs for distributed systems)
* Establish relationships (1:1, 1:N, N:M)
* Normalize to 3NF (unless denormalization needed for performance)
* Add appropriate indexes
* Define foreign key constraints
* Include timestamps (created_at, updated_at)
* Add soft delete flags if needed
* Plan for data archival
For NoSQL Databases :
* Design for access patterns (query-first approach)
* Embed vs reference decision
* Plan for denormalization
* Design indexes for common queries
* Consider document size limits
* Plan for eventual consistency
3. Generate Complete Schema :
SQL Schema Output :
-- [Entity Name] Table
-- Purpose: [Description]
CREATE TABLE [table_name] (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
[field_name] [TYPE] [CONSTRAINTS],
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMP
);
-- Indexes
CREATE INDEX idx_[table]_[field] ON [table]([field]);
CREATE INDEX idx_[table]_[field1]_[field2] ON [table]([field1], [field2]);
-- Foreign Keys
ALTER TABLE [child_table]
ADD CONSTRAINT fk_[constraint_name]
FOREIGN KEY ([foreign_key_field])
REFERENCES [parent_table](id)
ON DELETE CASCADE;
NoSQL Schema Output (MongoDB example):
// [Collection Name]
// Purpose: [Description]
{
_id: ObjectId,
[field_name]: [type],
// Embedded document
[embedded_object]: {
field1: type,
field2: type
},
// Reference
[related_id]: ObjectId, // Ref to [other_collection]
created_at: ISODate,
updated_at: ISODate
}
// Indexes
db.[collection].createIndex({ field: 1 })
db.[collection].createIndex({ field1: 1, field2: -1 })
db.[collection].createIndex({ field: "text" }) // Text search
4. Create Entity Relationship Diagram (text format):
┌─────────────────────┐
│ users │
├─────────────────────┤
│ id (PK) │
│ email (UNIQUE) │
│ name │
│ created_at │
└──────────┬──────────┘
│
│ 1:N
│
┌──────────▼──────────┐
│ posts │
├─────────────────────┤
│ id (PK) │
│ user_id (FK) │
│ title │
│ content │
│ created_at │
└──────────┬──────────┘
│
│ N:M (via post_tags)
│
┌──────────▼──────────┐
│ tags │
├─────────────────────┤
│ id (PK) │
│ name (UNIQUE) │
└─────────────────────┘
5. Provide Migration Scripts :
-- Migration: create_users_table
-- Date: 2024-01-15
BEGIN;
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_created_at ON users(created_at);
COMMIT;
-- Rollback
BEGIN;
DROP TABLE users;
COMMIT;
6. Format Complete Output :
🗄️ DATABASE SCHEMA DESIGN
Database: [PostgreSQL/MySQL/MongoDB/etc.]
Domain: [Application type]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📋 ENTITY RELATIONSHIP DIAGRAM
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[ASCII ERD]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 TABLE DEFINITIONS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[SQL CREATE TABLE statements]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔗 RELATIONSHIPS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[Foreign key constraints]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚡ INDEXES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[Index definitions with rationale]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔄 MIGRATION SCRIPTS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[Up and down migrations]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💡 OPTIMIZATION NOTES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Performance Considerations:
• [Index strategy]
• [Partitioning recommendations]
• [Denormalization opportunities]
Scaling Strategy:
• [Sharding approach]
• [Read replicas]
• [Caching layer]
Data Integrity:
• [Constraint strategy]
• [Validation rules]
• [Audit logging]
7. Schema Design Best Practices :
Naming Conventions :
* Use snake_case for table and column names
* Pluralize table names (users, posts)
* Use descriptive foreign key names (user_id, not uid)
* Prefix indexes (idx_table_column)
* Prefix constraints (fk_, uk_, ck_)
Data Types :
* Use appropriate types (INT vs BIGINT, VARCHAR vs TEXT)
* Consider storage size
* Use ENUM for fixed sets of values
* Use JSON/JSONB for flexible attributes
* Use proper date/time types (TIMESTAMP vs DATETIME)
Indexes :
* Index foreign keys
* Index columns in WHERE clauses
* Composite indexes for multi-column queries
* Consider covering indexes
* Monitor index usage and remove unused ones
Relationships :
* Always use foreign keys in relational DBs
* Cascade deletes where appropriate
* Consider soft deletes for audit trails
* Use junction tables for many-to-many
Performance :
* Denormalize for read-heavy workloads
* Partition large tables
* Use materialized views for complex queries
* Consider read replicas
* Plan for archival of old data
Ensure schemas:
Generate production-ready, optimized database schemas that scale.
Weekly Installs
69
Repository
GitHub Stars
75
First Seen
Jan 24, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode57
codex56
cursor56
gemini-cli55
claude-code55
github-copilot53
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
122,000 周安装
Figma 设计转代码技能:实现像素级精准开发与设计系统集成
1,500 周安装
无服务器与微服务开发指南:FastAPI、AWS Lambda、Azure Functions 架构实践
109 周安装
想法锦标赛:结构化研究提案生成框架,通过树状扩展与Elo评级选出最佳方案
69 周安装
语雀文档管理MCP工具使用指南:创建、搜索、更新、移动、删除文档
84 周安装
React Testing Library 最佳实践指南:编写可维护、以用户为中心的组件测试
111 周安装
Claude web-access 联网技能:浏览器自动化与网页抓取工具,支持CDP代理和Shell命令
1,800 周安装