home / skills / apconw / aix-db / schema-exploration

schema-exploration skill

/agent/deepagent/skills/schema-exploration

This skill helps you understand database structures by listing tables, detailing schemas, and mapping relationships for data insights.

npx playbooks add skill apconw/aix-db --skill schema-exploration

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.8 KB
---
name: schema-exploration
description: 用于发现和理解数据库结构、表、列和关系
---

# 架构探索技能

## 何时使用此技能

当您需要以下操作时使用此技能:
- 理解数据库结构
- 查找包含特定类型数据的表
- 发现列名和数据类型
- 映射表之间的关系
- 回答诸如"有哪些表可用?"或"Customer 表有哪些列?"等问题

## 工作流程

### 1. 列出所有表
使用 `sql_db_list_tables` 工具查看数据库中所有可用的表。

这将返回您可以查询的完整表列表。

### 2. 获取特定表的架构
使用 `sql_db_schema` 工具配合表名来检查:
- **列名** - 有哪些字段可用
- **数据类型** - INTEGER, TEXT, DATETIME 等
- **示例数据** - 3 行示例数据以了解内容
- **主键** - 行的唯一标识符
- **外键** - 与其他表的关系

### 3. 获取表关系
使用 `sql_db_table_relationship` 工具获取表之间的外键/关联关系:
- **关联字段** - 两表通过哪些字段关联
- **JOIN 条件** - 返回格式如 `t_orders.customer_id = t_customers.id`

示例调用:
```
sql_db_table_relationship("t_orders, t_customers, t_products")
```

如果未配置表关系,可以通过以下方式推断:
- 查找以 "Id" 或 "_id" 结尾的列(例如,customer_id, product_id)
- 外键列名通常对应另一个表的主键
- 记录父子关系

### 4. 回答问题
提供清晰的信息:
- 可用表及其用途
- 列名及其包含的内容
- 表之间的关联方式
- 示例数据以说明内容

## 示例:"有哪些表可用?"

**步骤 1:** 使用 `sql_db_list_tables`

**响应:**
```
数据库包含多个表:
1. Customer - 存储客户信息
2. Employee - 存储员工信息
3. Invoice - 客户购买记录
4. InvoiceLine - 发票中的单个项目
...
```

## 示例:"这些表之间有什么关系?"

**步骤 1:** 使用 `sql_db_table_relationship("Customer, Invoice, InvoiceLine")`

**响应:**
```
表之间的关系如下:
  • Invoice.CustomerId = Customer.Id
  • InvoiceLine.InvoiceId = Invoice.Id

✅ 表关系已获取完成。
```

## 示例:"Customer 表有哪些列?"

**步骤 1:** 使用 `sql_db_schema` 工具,表名为 "Customer"

**响应:**
```
Customer 表包含以下列:

- CustomerId (INTEGER) - 主键,唯一客户 ID
- FirstName (NVARCHAR) - 客户名字
- LastName (NVARCHAR) - 客户姓氏
- Company (NVARCHAR) - 公司名称(可选)
- Address, City, State, Country, PostalCode - 地址字段
- Phone (NVARCHAR) - 电话号码
- Email (NVARCHAR) - 电子邮件地址
- SupportRepId (INTEGER) - 指向 Employee 表的外键

示例数据显示了如下客户:
- 示例客户 1
- 示例客户 2
- 示例客户 3
```

## 质量指南

**对于"列出表"问题:**
- 显示所有表名
- 添加每个表包含内容的简要描述
- 对相关表进行分组(例如,交易、人员)

**对于"描述表"问题:**
- 列出所有列及其数据类型
- 解释每列包含的内容
- 显示示例数据以提供上下文
- 注明主键和外键
- 解释与其他表的关系

**对于"如何查询 X"问题:**
- 识别所需的表
- 映射 JOIN 路径
- 解释关系链
- 建议下一步(使用查询编写技能)

## 常见探索模式

### 模式 1:查找表
"哪个表包含客户信息?"
→ 使用 list_tables,然后描述 Customer 表

### 模式 2:理解结构
"Invoice 表中有什么?"
→ 使用 schema 工具显示列和示例数据

### 模式 3:映射关系
"客户如何与发票关联?"
→ 使用 `sql_db_table_relationship("Customer, Invoice, InvoiceLine")` 获取关系
→ 返回如:`Invoice.CustomerId = Customer.Id`, `InvoiceLine.InvoiceId = Invoice.Id`

## 提示

- 外键通常以 "Id" 结尾,并匹配表名
- 使用示例数据了解值的格式
- 不确定使用哪个表时,先列出所有表

Overview

This skill discovers and explains database structure, tables, columns, and relationships to turn schema into actionable insights. It helps you find where specific data lives, inspect column types and sample values, and map foreign-key links between tables. The output is practical and geared toward writing accurate queries or planning data models.

How this skill works

The skill lists available tables, inspects table schemas with column names, types, primary keys, and sample rows, and extracts declared or inferred table relationships. It uses heuristics (column names ending in Id or _id) plus explicit foreign-key metadata to produce JOIN conditions and relationship mappings. Responses focus on concise descriptions, sample data context, and clear next steps for querying.

When to use it

  • You need to understand the overall database layout before querying.
  • You want to find which table contains a particular type of data (e.g., customer emails).
  • You must map JOIN paths to write multi-table queries.
  • You need column-level details: data types, keys, and example values.
  • You want a quick reference of table purposes and how tables group by domain.

Best practices

  • Start by listing all tables to get domain groups (transactions, people, config).
  • Inspect schema for a specific table to see column types, PKs, and sample rows before querying.
  • Use table-relationship output to derive safe JOIN conditions and query paths.
  • When relationships are not explicit, look for columns ending with Id or _id and match them to candidate primary keys.
  • Include sample rows in descriptions to validate assumptions about value formats.

Example use cases

  • Ask “Which tables contain customer information?” and get table names plus short descriptions.
  • Request schema for Customer to receive column names, types, primary/foreign keys, and three example rows.
  • Get relationship map for Customer, Invoice, InvoiceLine to obtain JOIN conditions like Invoice.CustomerId = Customer.Id.
  • Discover which table stores product SKUs and the column type to prepare filtering or aggregation queries.
  • Identify foreign keys and next-step suggestions for building a text2sql or analytical query.

FAQ

How does the skill infer relationships if no FK metadata exists?

It looks for naming patterns (Id, _id), matches candidate columns to primary keys in other tables, and reports likely JOIN conditions with confidence notes.

Will I get sample data with every schema inspection?

Yes — the skill returns a small number of example rows (typically three) to help verify value formats and content.