Database Design Skill logo

Database Design Skill

Community
jiushiwon
database-design-skill

数据库设计规范技能。面向 AI 生成代码,提供表名/字段名规范、索引设计规范、关联规范、性能优化规范、分库分表规范、引擎选择等统一标准。被所有后端 skill 引用,确保 AI 生成的代码遵循统一数据库设计规范。触发词:"数据库设计规范"、"表名规范"、"字段名规范"、"索引设计"、"分库分表"、"数据库性能优化"。

Overview

Publisherjiushiwon
Repositorywg-skills
Skill namedatabase-design-skill
Stars
123
Forks
38
Bundled files
6
LicenseApache-2.0
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 6 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by jiushiwon on GitHub. Read the source before you install it.

Installation

Install the Database Design Skill AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/jiushiwon/wg-skills/tree/main/vibeCoding/backend/database/database-design-skill
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/jiushiwon/wg-skills.git /tmp/wg-skills
mkdir -p .claude/skills
cp -r /tmp/wg-skills/vibeCoding/backend/database/database-design-skill .claude/skills/database-design-skill
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Database Design Skill in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Database Design Skill on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Database Design Skill is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Database Design Skill

数据库设计规范体系——AI 生成代码时必须遵循的统一标准。

核心理念

AI 天然会建表,但建得好不好是另一回事。

本 skill 定义了一套完整的数据库设计规范,确保 AI 生成的后端项目:

  • 表结构统一、规范、易维护
  • 索引合理、性能有保障
  • 关联清晰、不易出错
  • 支持扩展、能应对分库分表

一、表名与字段名规范

1.1 命名原则

原则说明示例
英文小写全部小写,用下划线分隔user_name,不是 userName
有含义用完整英文单词,不用缩写created_at,不是 ctime
表前缀统一前缀,默认 wg_wg_user,wg_order
单数名词表名用单数wg_user,不是 wg_users

1.2 基础字段(所有表必须有)

sql
-- 所有业务表必须包含这些字段
CREATE TABLE "wg_user" (
    "id" BIGSERIAL PRIMARY KEY,
    "status" SMALLINT NOT NULL DEFAULT 1 COMMENT '状态:0禁用 1正常',
    "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
    "deleted_at" TIMESTAMP DEFAULT NULL COMMENT '删除时间(软删除)'
);

COMMENT ON TABLE "wg_user" IS '用户表';
COMMENT ON COLUMN "wg_user"."status" IS '状态:0-禁用 1-正常';
COMMENT ON COLUMN "wg_user"."deleted_at" IS '软删除时间戳';

1.3 字段类型选择

数据类型MySQLPostgreSQL场景
主键BIGINT UNSIGNEDBIGSERIAL自增主键
状态TINYINTSMALLINT0/1/2 等状态码
布尔TINYINT(1)BOOLEANtrue/false
时间DATETIMETIMESTAMP创建/更新时间
枚举ENUM / TINYINTCHECK 约束有限选项
大文本TEXTTEXT长文本
JSONJSON / JSONBJSONB动态结构(PG 推荐)

二、索引设计规范

2.1 索引类型选择

索引场景示例
主键索引id 字段,自动唯一PRIMARY KEY
唯一索引唯一约束字段uk_username on username
普通索引WHERE 常用字段idx_status on status
复合索引多条件查询idx_status_created on (status, created_at)

2.2 复合索引最左前缀原则

sql
-- 复合索引 (status, created_at, user_id)
-- ✅ 能命中:WHERE status = 1
-- ✅ 能命中:WHERE status = 1 AND created_at > '2024-01-01'
-- ✅ 能命中:WHERE status = 1 AND created_at > '2024-01-01' AND user_id = 1
-- ❌ 不能命中:WHERE created_at > '2024-01-01'
-- ❌ 不能命中:WHERE user_id = 1

2.3 索引设计原则

sql
-- 必须加索引的场景
-- 1. WHERE 条件字段
-- 2. ORDER BY 排序字段
-- 3. JOIN 连接字段
-- 4. 唯一性约束字段

-- 示例:用户表完整索引
CREATE TABLE "wg_user" (
    "id" BIGSERIAL PRIMARY KEY,
    "username" VARCHAR(50) NOT NULL,
    "email" VARCHAR(100),
    "phone" VARCHAR(20),
    "status" SMALLINT NOT NULL DEFAULT 1,
    "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted_at" TIMESTAMP
);

-- 索引
ALTER TABLE "wg_user" ADD CONSTRAINT "uk_username" UNIQUE ("username");
CREATE INDEX "idx_email" ON "wg_user" ("email");
CREATE INDEX "idx_phone" ON "wg_user" ("phone");
CREATE INDEX "idx_status" ON "wg_user" ("status");
CREATE INDEX "idx_created_at" ON "wg_user" ("created_at");
CREATE INDEX "idx_status_created" ON "wg_user" ("status", "created_at");

三、关联规范

3.1 外键使用原则

sql
-- 推荐:业务层控制关联,数据库层面不强制外键(性能考虑)
-- 但保留外键约束文档,供 DBA 审阅

-- 订单表关联用户
CREATE TABLE "wg_order" (
    "id" BIGSERIAL PRIMARY KEY,
    "user_id" BIGINT NOT NULL COMMENT '用户ID',
    "order_no" VARCHAR(32) NOT NULL COMMENT '订单号',
    "amount" DECIMAL(10,2) NOT NULL DEFAULT 0 COMMENT '金额',
    "status" SMALLINT NOT NULL DEFAULT 1 COMMENT '状态',
    "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted_at" TIMESTAMP
);

-- 索引替代外键(性能更好)
CREATE INDEX "idx_user_id" ON "wg_order" ("user_id");
CREATE INDEX "idx_order_no" ON "wg_order" ("order_no" UNIQUE);
CREATE INDEX "idx_status" ON "wg_order" ("status");

3.2 关联查询规范

sql
-- ❌ 避免:深度嵌套 JOIN(超过 3 表)
-- ✅ 推荐:分步查询或使用中间表

-- 订单详情查询示例
SELECT o.*, u.username, u.nickname
FROM wg_order o
LEFT JOIN wg_user u ON o.user_id = u.id AND u.deleted_at IS NULL
WHERE o.id = $1 AND o.deleted_at IS NULL;

四、性能优化规范

4.1 查询优化规则

规则说明示例
**避免 SELECT ***只查需要的字段SELECT id, username FROM users
避免函数运算函数会导致索引失效WHERE YEAR(created_at) = 2024 ❌
避免隐式转换类型要匹配WHERE phone = 13800138000 ❌
使用分页游标大偏移量用游标WHERE id > last_id 替代 LIMIT 100000, 10
批量操作减少数据库交互INSERT/DELETE 批量执行

4.2 慢查询分析

sql
-- MySQL 慢查询分析
SHOW VARIABLES LIKE 'slow_query_log';
SELECT * FROM mysql.slow_log ORDER BY start_time DESC LIMIT 10;

-- PostgreSQL 慢查询分析
SELECT * FROM pg_stat_statements ORDER BY total_time DESC LIMIT 10;

-- EXPLAIN 分析查询
EXPLAIN ANALYZE SELECT * FROM wg_user WHERE username = 'test';

4.3 分页优化

sql
-- ❌ 慢:OFFSET 大时性能差
SELECT * FROM wg_order ORDER BY id LIMIT 10 OFFSET 100000;

-- ✅ 快:基于游标的分页
SELECT * FROM wg_order 
WHERE id > 100000 
ORDER BY id 
LIMIT 10;

-- ✅ 适合条件筛选
SELECT * FROM wg_order 
WHERE status = 1 AND id > #{lastId}
ORDER BY id 
LIMIT 10;

五、分库分表规范

5.1 分库分表场景

场景策略说明
数据量 < 1000万不分单表足以
1000万 ~ 1亿分表水平分表
1亿以上分库分表分布式架构

5.2 分片键选择

sql
-- 常见分片键
-- 1. 用户ID:按用户维度查询
-- 2. 时间:按时间范围查询(如日志)
-- 3. 地区:按地域查询

-- 示例:订单表按 user_id 分片
CREATE TABLE "wg_order" (
    "id" BIGINT NOT NULL,
    "user_id" BIGINT NOT NULL,
    "order_no" VARCHAR(32) NOT NULL,
    ...
    PRIMARY KEY ("id", "user_id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
PARTITION BY HASH("user_id") PARTITIONS 16;

5.3 分库分表中间件

中间件特点适用场景
ShardingSphere功能完善,支持多语言Java 项目
ShardingJDBC轻量,Java 直连Java 项目
MyCat代理模式多语言项目
VitessMySQL 分布式规模大

六、数据库引擎与配置

6.1 MySQL 引擎选择

引擎特点场景
InnoDB支持事务、行锁(默认)绝大多数场景
MyISAM不支持事务、全文索引只读/日志
Memory内存存储临时表、缓存
sql
-- 创建 InnoDB 表(默认)
CREATE TABLE "wg_user" (
    "id" BIGINT PRIMARY KEY
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

6.2 PostgreSQL 配置

sql
-- 建议配置
-- 1. 主键使用 BIGSERIAL(自增)
-- 2. 时间使用 TIMESTAMP WITH TIME ZONE
-- 3. 文本使用 TEXT(无长度限制)
-- 4. JSON 优先使用 JSONB(索引支持)

七、交付规范

本 skill 不生成独立文件,决策结果必须落到:

  1. spec.md(选型期)

    • 数据库类型(MySQL/PG/MongoDB)
    • 表前缀(如 wg_)
    • 核心实体清单
    • 是否需要分库分表
  2. 生成项目内

    • 连接配置(.env.example)
    • DDL 迁移文件
    • entity 定义

引用索引

文件内容
references/naming-convention.md表名/字段名详细规范
references/index-design.md索引设计详解
references/sharding-guide.md分库分表实战指南
references/performance.md性能优化实战
references/migration-guide.md迁移规范(Flyway/Alembic)

不做

  • 不负责数据库安装(使用 database-install-skill)
  • 不处理数据库集群高可用
  • 不提供具体业务表设计(由业务 skill 决定)
  • 不替代 DBA 深度优化

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Database Design Skill AI skill do?

数据库设计规范技能。面向 AI 生成代码,提供表名/字段名规范、索引设计规范、关联规范、性能优化规范、分库分表规范、引擎选择等统一标准。被所有后端 skill 引用,确保 AI 生成的代码遵循统一数据库设计规范。触发词:"数据库设计规范"、"表名规范"、"字段名规范"、"索引设计"、"分库分表"、"数据库性能优化"。

Why use Database Design Skill on TypingMind?

Because you install it once and use it with any model. Database Design Skill is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Database Design Skill in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/jiushiwon/wg-skills/tree/main/vibeCoding/backend/database/database-design-skill. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Database Design Skill?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Database Design Skill?

As many as you like. As long as a model supports skills, you can use Database Design Skill with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Database Design Skill AI skill free?

Yes. It is published on GitHub by jiushiwon under the Apache-2.0 license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇