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

# 6.2. 更新資料 \#

### 6.2. 更新資料 [#](#dml-update) <a href="#dml-update" id="dml-update"></a>

修改已經存在於資料庫中的資料，稱為更新。你可以更新個別的資料列、資料表中的所有資料列，或所有資料列的某個子集。每個欄位都可以個別更新；其他欄位不受影響。

要更新現有的資料列，請使用 [UPDATE](https://docs.postgresql.tw/19/reference/sql-commands/sql-update.md) 命令。這需要三項資訊：

1. 要更新的資料表與欄位名稱
2. 欄位的新值
3. 要更新哪一筆（或哪些）資料列

回想[第 5 章](https://docs.postgresql.tw/19/the-sql-language/ddl.md)提過，SQL 一般而言並不為資料列提供唯一識別碼。因此，不一定總能直接指定要更新哪一筆資料列。取而代之的是，你要指定資料列必須滿足哪些條件才會被更新。只有當資料表中有主鍵時（無論你是否宣告了它），你才能藉由選擇與主鍵相符的條件，可靠地指定個別的資料列。圖形化的資料庫存取工具就是依賴這個事實，讓你能夠逐筆更新資料列。

例如，下面這個命令會把所有價格為 5 的產品，價格更新為 10：

```

UPDATE products SET price = 10 WHERE price = 5;
```

這可能會使零筆、一筆或多筆資料列被更新。嘗試進行一個不符合任何資料列的更新並不是錯誤。

讓我們仔細看看這個命令。首先是關鍵字 `UPDATE`，後面接著資料表名稱。和平常一樣，資料表名稱可以用綱要限定，否則會在搜尋路徑中查找。接著是關鍵字 `SET`，後面接著欄位名稱、等號與新的欄位值。新的欄位值可以是任何純量運算式，而不只是常數。例如，如果你想把所有產品的價格提高 10%，可以使用：

```

UPDATE products SET price = price * 1.10;
```

如你所見，新值的運算式可以參照資料列中現有的值。我們也省略了 `WHERE` 子句。如果省略它，就表示資料表中的所有資料列都會被更新。如果有這個子句，就只有符合 `WHERE` 條件的資料列會被更新。請注意，`SET` 子句中的等號是指派，而 `WHERE` 子句中的等號是比較，但這並不會造成任何歧義。當然，`WHERE` 條件不一定要是相等測試。還有許多其他運算子可用（請參閱[第 9 章](https://docs.postgresql.tw/19/the-sql-language/functions.md)）。但運算式的結果必須是布林值。

你可以在 `SET` 子句中列出多個指派，在一個 `UPDATE` 命令中更新多個欄位。例如：

```

UPDATE mytable SET a = 5, b = 3, c = 1 WHERE a > 0;
```

***

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


---

# 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/dml/dml-update.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.
