# 資料庫

<figure><img src="https://1593648278-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fmzb5NG9GDzFP2YDKeYVl%2Fuploads%2Fgit-blob-d7e628289c0aad13324d2c0a9a839792886b37f0%2Fdatabase-infographic.png?alt=media" alt="資料庫概念"><figcaption><p>Text to SQL：自然語言 → SQL 查詢 → 白話回覆</p></figcaption></figure>

## 這是什麼？

資料庫讓 Agent 能用自然語言查詢結構化數據。使用者問「上個月我們賣了多少台筆電？」，Agent 會自動把這句話轉成 SQL 查詢，從資料庫撈出答案再用白話回覆。

這個功能的技術名稱是 **Text to SQL**。

## 什麼時候需要用？

當你的場景涉及：

* **數據查詢**：訂單狀態、庫存數量、銷售額
* **統計分析**：「本季業績比上季成長多少？」
* **即時狀態**：「目前有幾筆待處理的工單？」

這些問題的答案存在結構化的表格裡，不是在文件中，所以需要資料庫而非知識庫。

## 支援什麼資料庫？

| 類型                 | 說明                                       |
| ------------------ | ---------------------------------------- |
| **MaiAgent 內建資料庫** | 平台自帶的資料庫，可直接上傳 CSV 或手動建表                 |
| **外部資料庫**          | 連線到你的 PostgreSQL、MySQL、SQL Server、Oracle |

## 安全嗎？

Agent 只會執行**唯讀查詢**（SELECT），不會修改或刪除你的資料。你也可以限制 Agent 能存取的表格和欄位。

> **進階說明**：Text to SQL 的準確度取決於表格結構的清晰度。建議為表格和欄位加上中文描述，幫助 Agent 理解每個欄位代表什麼含義。

## 我需要做什麼？

1. **建立資料庫連線** — 選擇資料庫類型、設定連線資訊
2. **設定可存取的表格** — 選擇 Agent 能查詢哪些表
3. **加欄位描述** — 為欄位加上說明，提升查詢準確度
4. **測試查詢** — 用自然語言測試 Agent 能不能正確查詢
5. **掛載到 Agent** — 以工具形式連結到 Agent

## 延伸閱讀

* [Text to SQL 功能](https://github.com/Playma-Co-Ltd/maiagent-user-guide-gitbook/blob/main/zh-tw/tools/text2sql.md)
* [使用 MaiAgent 知識庫進行 Text to SQL](https://github.com/Playma-Co-Ltd/maiagent-user-guide-gitbook/blob/main/zh-tw/tools/text-to-sql-maiagent.md)
* [使用 Supabase 進行 Text to SQL](https://github.com/Playma-Co-Ltd/maiagent-user-guide-gitbook/blob/main/zh-tw/tools/text-to-sql-supabase.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.maiagent.ai/agent-builder/database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
