> 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/ddl/ddl-identity-columns.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 5.3. 識別欄位 \#

### 5.3. 識別欄位 [#](#ddl-identity-columns) <a href="#ddl-identity-columns" id="ddl-identity-columns"></a>

識別欄位是一種特殊的欄位，它的值會由一個隱含的序列自動產生。它可以用來產生鍵值。

若要建立識別欄位，請在 `CREATE TABLE` 中使用 `GENERATED ... AS IDENTITY` 子句，例如：

```

CREATE TABLE people (
    id bigint GENERATED ALWAYS AS IDENTITY,
    ...,
);
```

或者也可以寫成

```

CREATE TABLE people (
    id bigint GENERATED BY DEFAULT AS IDENTITY,
    ...,
);
```

更多細節請參閱 [CREATE TABLE](https://docs.postgresql.tw/19/reference/sql-commands/sql-createtable.md)。

如果對具有識別欄位的資料表執行 `INSERT` 指令，而且沒有為該識別欄位明確指定值，那麼就會插入由隱含序列所產生的值。例如，在上述定義之下，並假設還有其他適當的欄位，寫成

```

INSERT INTO people (name, address) VALUES ('A', 'foo');
INSERT INTO people (name, address) VALUES ('B', 'bar');
```

會為 `id` 欄位產生從 1 開始的值，並得到下列的資料表內容：

```

 id | name | address
----+------+---------
  1 | A    | foo
  2 | B    | bar
```

另一種做法是，可以用關鍵字 `DEFAULT` 取代值的位置，明確要求使用序列所產生的值，例如

```

INSERT INTO people (id, name, address) VALUES (DEFAULT, 'C', 'baz');
```

同樣地，關鍵字 `DEFAULT` 也可以用在 `UPDATE` 指令中。

因此在許多方面，識別欄位的行為都很像是一個具有預設值的欄位。

欄位定義中的 `ALWAYS` 與 `BY DEFAULT` 子句，決定了在 `INSERT` 與 `UPDATE` 指令中要如何處理使用者明確指定的值。在 `INSERT` 指令中，若選擇了 `ALWAYS`，則只有當 `INSERT` 陳述式指定了 `OVERRIDING SYSTEM VALUE` 時，才會接受使用者指定的值。若選擇了 `BY DEFAULT`，則使用者指定的值優先。因此，使用 `BY DEFAULT` 所得到的行為比較接近預設值的行為，也就是預設值可以被明確指定的值覆寫；而 `ALWAYS` 則提供了較多的保護，避免不小心插入明確的值。

識別欄位的資料型別必須是序列所支援的資料型別之一。（請參閱 [CREATE SEQUENCE](https://docs.postgresql.tw/19/reference/sql-commands/sql-createsequence.md)。）相關聯序列的屬性可以在建立識別欄位時指定（請參閱 [CREATE TABLE](https://docs.postgresql.tw/19/reference/sql-commands/sql-createtable.md)），或是在之後變更（請參閱 [ALTER TABLE](https://docs.postgresql.tw/19/reference/sql-commands/sql-altertable.md)）。

識別欄位會自動被標記為 `NOT NULL`。不過，識別欄位並不保證唯一性。（序列通常會回傳唯一的值，但序列可能被重設，或者如前面所述，也可能有值被手動插入識別欄位中。）唯一性必須透過 `PRIMARY KEY` 或 `UNIQUE` 限制條件來強制要求。

在資料表繼承階層中，子資料表的識別欄位及其屬性，與其父資料表中的識別欄位及其屬性完全獨立。子資料表不會自動從父資料表繼承識別欄位或其屬性。在 `INSERT` 或 `UPDATE` 進行期間，只要某個欄位在陳述式所指名的資料表中是識別欄位，該欄位就會被當作識別欄位處理，並套用對應的識別屬性。

分割區會從分割資料表繼承識別欄位。它們不能擁有自己的識別欄位。在分割階層中，某個識別欄位的屬性在所有分割區之間都是一致的。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/ddl-identity-columns.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/19/the-sql-language/ddl/ddl-identity-columns.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.
