> 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/19/the-sql-language/indexes/indexes-unique.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 11.6. 唯一值索引

### 11.6. 唯一值索引 [#](#indexes-unique) <a href="#indexes-unique" id="indexes-unique"></a>

索引也可用來強制欄位值唯一，或強制多個欄位組合值唯一。

```

CREATE UNIQUE INDEX name ON table (column [, ...]) [ NULLS [ NOT ] DISTINCT ];
```

目前只有 B-tree 索引可宣告為唯一值索引。

將索引宣告為唯一值索引後，不允許多筆資料表資料列具有相同索引值。預設情況下，唯一欄位中的 null 值不視為相等，因此欄位可包含多個 null。`NULLS NOT DISTINCT` 選項會改變此行為，讓索引將 null 視為相等。多欄位唯一值索引只會拒絕多筆資料列中所有已建立索引欄位皆相等的情況。

在資料表上定義唯一限制條件或主鍵時，PostgreSQL 會自動建立唯一值索引。該索引涵蓋組成主鍵或唯一限制條件的欄位（必要時為多欄位索引），並藉此強制執行限制條件。

#### 注意

無須手動在唯一欄位上建立索引；這只會複製自動建立的索引。

***

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


---

# 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/19/the-sql-language/indexes/indexes-unique.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.
