> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/18/the-sql-language.md).

# 第二部分：SQL 語言

## 第二部分：SQL 語言

本部分說明如何在 PostgreSQL 中使用 SQL 語言。我們先介紹 SQL 的一般語法，接著說明如何建立資料表、如何將資料填入資料庫，以及如何查詢資料庫。中間的章節列出可在 SQL 指令中使用的資料型別與函式。最後，我們會探討幾個對於調校資料庫而言相當重要的面向。

這些資訊的編排方式，讓初學者可以從頭讀到尾，在不需要太常往前查閱的情況下完整理解各個主題。各章也力求自成一體，因此進階使用者可以依自己的需要單獨閱讀某一章。內容以敘述的形式依主題單元呈現。想要查閱某個指令完整說明的讀者，建議參閱[第六部分](https://docs.postgresql.tw/18/reference.md)。

讀者應該知道如何連線到 PostgreSQL 資料庫並發出 SQL 指令。對這些事情還不熟悉的讀者，建議先閱讀[第一部分](https://docs.postgresql.tw/18/tutorial.md)。SQL 指令通常是使用 PostgreSQL 的互動式終端機程式 psql 輸入的，但也可以使用其他具有類似功能的程式。

**目錄**

* [4. SQL 語法](https://docs.postgresql.tw/18/the-sql-language/sql-syntax.md)
  * [4.1. 詞彙結構](https://docs.postgresql.tw/18/the-sql-language/sql-syntax/sql-syntax-lexical.md)
  * [4.2. 值運算式](https://docs.postgresql.tw/18/the-sql-language/sql-syntax/sql-expressions.md)
  * [4.3. 呼叫函式](https://docs.postgresql.tw/18/the-sql-language/sql-syntax/sql-syntax-calling-funcs.md)
* [5. 資料定義](https://docs.postgresql.tw/18/the-sql-language/ddl.md)
  * [5.1. 資料表基礎](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-basics.md)
  * [5.2. 預設值](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-default.md)
  * [5.3. 識別欄位](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-identity-columns.md)
  * [5.4. 產生欄位](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-generated-columns.md)
  * [5.5. 限制條件](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-constraints.md)
  * [5.6. 系統欄位](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-system-columns.md)
  * [5.7. 修改資料表](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-alter.md)
  * [5.8. 權限](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-priv.md)
  * [5.9. 資料列安全政策](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-rowsecurity.md)
  * [5.10. 綱要](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-schemas.md)
  * [5.11. 繼承](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-inherit.md)
  * [5.12. 資料表分割](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-partitioning.md)
  * [5.13. 外部資料](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-foreign-data.md)
  * [5.14. 其他資料庫物件](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-others.md)
  * [5.15. 相依性追蹤](https://docs.postgresql.tw/18/the-sql-language/ddl/ddl-depend.md)
* [6. 資料操作](https://docs.postgresql.tw/18/the-sql-language/dml.md)
  * [6.1. 插入資料](https://docs.postgresql.tw/18/the-sql-language/dml/dml-insert.md)
  * [6.2. 更新資料](https://docs.postgresql.tw/18/the-sql-language/dml/dml-update.md)
  * [6.3. 刪除資料](https://docs.postgresql.tw/18/the-sql-language/dml/dml-delete.md)
  * [6.4. 從修改的資料列回傳資料](https://docs.postgresql.tw/18/the-sql-language/dml/dml-returning.md)
* [7. 查詢](https://docs.postgresql.tw/18/the-sql-language/queries.md)
  * [7.1. 概觀](https://docs.postgresql.tw/18/the-sql-language/queries/queries-overview.md)
  * [7.2. 資料表運算式](https://docs.postgresql.tw/18/the-sql-language/queries/queries-table-expressions.md)
  * [7.3. 選取清單](https://docs.postgresql.tw/18/the-sql-language/queries/queries-select-lists.md)
  * [7.4. 組合查詢（`UNION`、`INTERSECT`、`EXCEPT`）](https://docs.postgresql.tw/18/the-sql-language/queries/queries-union.md)
  * [7.5. 排序資料列（`ORDER BY`）](https://docs.postgresql.tw/18/the-sql-language/queries/queries-order.md)
  * [7.6. `LIMIT` 與 `OFFSET`](https://docs.postgresql.tw/18/the-sql-language/queries/queries-limit.md)
  * [7.7. `VALUES` 清單](https://docs.postgresql.tw/18/the-sql-language/queries/queries-values.md)
  * [7.8. `WITH` 查詢（通用資料表運算式）](https://docs.postgresql.tw/18/the-sql-language/queries/queries-with.md)
* [8. 資料型別](https://docs.postgresql.tw/18/the-sql-language/datatype.md)
  * [8.1. 數值型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-numeric.md)
  * [8.2. 貨幣型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-money.md)
  * [8.3. 字元型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-character.md)
  * [8.4. 二進位資料型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-binary.md)
  * [8.5. 日期／時間型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-datetime.md)
  * [8.6. 布林型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-boolean.md)
  * [8.7. 列舉型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-enum.md)
  * [8.8. 幾何型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-geometric.md)
  * [8.9. 網路位址型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-net-types.md)
  * [8.10. 位元字串型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-bit.md)
  * [8.11. 全文檢索型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-textsearch.md)
  * [8.12. UUID 型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-uuid.md)
  * [8.13. XML 型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-xml.md)
  * [8.14. JSON 型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-json.md)
  * [8.15. 陣列](https://docs.postgresql.tw/18/the-sql-language/datatype/arrays.md)
  * [8.16. 複合型別](https://docs.postgresql.tw/18/the-sql-language/datatype/rowtypes.md)
  * [8.17. 範圍型別](https://docs.postgresql.tw/18/the-sql-language/datatype/rangetypes.md)
  * [8.18. 網域型別](https://docs.postgresql.tw/18/the-sql-language/datatype/domains.md)
  * [8.19. 物件識別碼型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-oid.md)
  * [8.20. `pg_lsn` 型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-pg-lsn.md)
  * [8.21. 虛擬型別](https://docs.postgresql.tw/18/the-sql-language/datatype/datatype-pseudo.md)
* [9. 函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions.md)
  * [9.1. 邏輯運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-logical.md)
  * [9.2. 比較函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-comparison.md)
  * [9.3. 數學函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-math.md)
  * [9.4. 字串函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-string.md)
  * [9.5. 二進位字串函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-binarystring.md)
  * [9.6. 位元字串函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-bitstring.md)
  * [9.7. 模式比對](https://docs.postgresql.tw/18/the-sql-language/functions/functions-matching.md)
  * [9.8. 資料型別格式化函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-formatting.md)
  * [9.9. 日期／時間函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-datetime.md)
  * [9.10. 列舉支援函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-enum.md)
  * [9.11. 幾何函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-geometry.md)
  * [9.12. 網路位址函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-net.md)
  * [9.13. 文字搜尋函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-textsearch.md)
  * [9.14. UUID 函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-uuid.md)
  * [9.15. XML 函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-xml.md)
  * [9.16. JSON 函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-json.md)
  * [9.17. 序列操作函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-sequence.md)
  * [9.18. 條件運算式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-conditional.md)
  * [9.19. 陣列函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-array.md)
  * [9.20. 範圍／多重範圍函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-range.md)
  * [9.21. 彙總函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-aggregate.md)
  * [9.22. Window 函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-window.md)
  * [9.23. 合併支援函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-merge-support.md)
  * [9.24. 子查詢運算式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-subquery.md)
  * [9.25. 資料列與陣列比較](https://docs.postgresql.tw/18/the-sql-language/functions/functions-comparisons.md)
  * [9.26. 集合回傳函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-srf.md)
  * [9.27. 系統資訊函式與運算子](https://docs.postgresql.tw/18/the-sql-language/functions/functions-info.md)
  * [9.28. 系統管理函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-admin.md)
  * [9.29. 觸發程序函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-trigger.md)
  * [9.30. 事件觸發程序函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-event-triggers.md)
  * [9.31. 統計資訊函式](https://docs.postgresql.tw/18/the-sql-language/functions/functions-statistics.md)
* [10. 型別轉換](https://docs.postgresql.tw/18/the-sql-language/typeconv.md)
  * [10.1. 概觀](https://docs.postgresql.tw/18/the-sql-language/typeconv/typeconv-overview.md)
  * [10.2. 運算子](https://docs.postgresql.tw/18/the-sql-language/typeconv/typeconv-oper.md)
  * [10.3. 函式](https://docs.postgresql.tw/18/the-sql-language/typeconv/typeconv-func.md)
  * [10.4. 值的儲存](https://docs.postgresql.tw/18/the-sql-language/typeconv/typeconv-query.md)
  * [10.5. `UNION`、`CASE` 與相關結構](https://docs.postgresql.tw/18/the-sql-language/typeconv/typeconv-union-case.md)
  * [10.6. `SELECT` 輸出欄位](https://docs.postgresql.tw/18/the-sql-language/typeconv/typeconv-select.md)
* [11. 索引](https://docs.postgresql.tw/18/the-sql-language/indexes.md)
  * [11.1. 簡介](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-intro.md)
  * [11.2. 索引類型](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-types.md)
  * [11.3. 多欄位索引](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-multicolumn.md)
  * [11.4. 索引與 `ORDER BY`](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-ordering.md)
  * [11.5. 組合多個索引](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-bitmap-scans.md)
  * [11.6. 唯一值索引](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-unique.md)
  * [11.7. 運算式索引](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-expressional.md)
  * [11.8. 部分索引](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-partial.md)
  * [11.9. 僅索引掃描與涵蓋索引](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-index-only-scans.md)
  * [11.10. 運算子類別與運算子族系](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-opclass.md)
  * [11.11. 索引與定序](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-collations.md)
  * [11.12. 檢查索引的使用情形](https://docs.postgresql.tw/18/the-sql-language/indexes/indexes-examine.md)
* [12. 全文檢索](https://docs.postgresql.tw/18/the-sql-language/textsearch.md)
  * [12.1. 簡介](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-intro.md)
  * [12.2. 資料表與索引](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-tables.md)
  * [12.3. 控制文字搜尋](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-controls.md)
  * [12.4. 其他功能](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-features.md)
  * [12.5. 剖析器](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-parsers.md)
  * [12.6. 字典](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-dictionaries.md)
  * [12.7. 設定範例](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-configuration.md)
  * [12.8. 測試與除錯文字搜尋](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-debugging.md)
  * [12.9. 文字搜尋的建議索引型別](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-indexes.md)
  * [12.10. psql 支援](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-psql.md)
  * [12.11. 限制](https://docs.postgresql.tw/18/the-sql-language/textsearch/textsearch-limitations.md)
* [13. 並行控制](https://docs.postgresql.tw/18/the-sql-language/mvcc.md)
  * [13.1. 簡介](https://docs.postgresql.tw/18/the-sql-language/mvcc/mvcc-intro.md)
  * [13.2. 交易隔離](https://docs.postgresql.tw/18/the-sql-language/mvcc/transaction-iso.md)
  * [13.3. 明確鎖定](https://docs.postgresql.tw/18/the-sql-language/mvcc/explicit-locking.md)
  * [13.4. 應用程式層級的資料一致性檢查](https://docs.postgresql.tw/18/the-sql-language/mvcc/applevel-consistency.md)
  * [13.5. 序列化失敗的處理](https://docs.postgresql.tw/18/the-sql-language/mvcc/mvcc-serialization-failure-handling.md)
  * [13.6. 注意事項](https://docs.postgresql.tw/18/the-sql-language/mvcc/mvcc-caveats.md)
  * [13.7. 鎖定與索引](https://docs.postgresql.tw/18/the-sql-language/mvcc/locking-indexes.md)
* [14. 效能提示](https://docs.postgresql.tw/18/the-sql-language/performance-tips.md)
  * [14.1. 使用 `EXPLAIN`](https://docs.postgresql.tw/18/the-sql-language/performance-tips/using-explain.md)
  * [14.2. 規劃器使用的統計資訊](https://docs.postgresql.tw/18/the-sql-language/performance-tips/planner-stats.md)
  * [14.3. 以明確的 `JOIN` 子句控制規劃器](https://docs.postgresql.tw/18/the-sql-language/performance-tips/explicit-joins.md)
  * [14.4. 填入資料庫](https://docs.postgresql.tw/18/the-sql-language/performance-tips/populate.md)
  * [14.5. 非持久性設定](https://docs.postgresql.tw/18/the-sql-language/performance-tips/non-durability.md)
* [15. 平行查詢](https://docs.postgresql.tw/18/the-sql-language/parallel-query.md)
  * [15.1. 平行查詢的運作方式](https://docs.postgresql.tw/18/the-sql-language/parallel-query/how-parallel-query-works.md)
  * [15.2. 何時可以使用平行查詢？](https://docs.postgresql.tw/18/the-sql-language/parallel-query/when-can-parallel-query-be-used.md)
  * [15.3. 平行計畫](https://docs.postgresql.tw/18/the-sql-language/parallel-query/parallel-plans.md)
  * [15.4. 平行安全性](https://docs.postgresql.tw/18/the-sql-language/parallel-query/parallel-safety.md)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/sql.html)（原文版本：18.6；核對日期：2026-09-13）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/18/the-sql-language.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
