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

# 8.6. 布林型別 \#

### 8.6. 布林型別 [#](#datatype-boolean) <a href="#datatype-boolean" id="datatype-boolean"></a>

PostgreSQL 提供標準 SQL 的 `boolean` 型別；請參閱[表 8.19](#datatype-boolean-table)。`boolean` 型別可以有數種狀態：「true」、「false」，以及第三種狀態「unknown」，後者是以 SQL 的空值（null）來表示。

#### 表 8.19. 布林資料型別 [#](#datatype-boolean-table) <a href="#datatype-boolean-table" id="datatype-boolean-table"></a>

| 名稱        | 儲存空間大小 | 說明               |
| --------- | ------ | ---------------- |
| `boolean` | 1 個位元組 | true 或 false 的狀態 |

<br>

在 SQL 查詢中，布林常數可以用 SQL 關鍵字 `TRUE`、`FALSE` 與 `NULL` 來表示。

`boolean` 型別的資料型別輸入函式接受下列這些代表「true」狀態的字串表示法：

| `true` |
| ------ |
| `yes`  |
| `on`   |
| `1`    |

以及下列這些代表「false」狀態的表示法：

| `false` |
| ------- |
| `no`    |
| `off`   |
| `0`     |

這些字串的唯一前綴也同樣被接受，例如 `t` 或 `n`。開頭或結尾的空白會被忽略，而且不區分大小寫。

`boolean` 型別的資料型別輸出函式一律只輸出 `t` 或 `f`，如[範例 8.2](#datatype-boolean-example)所示。

#### 範例 8.2. 使用 `boolean` 型別 [#](#datatype-boolean-example) <a href="#datatype-boolean-example" id="datatype-boolean-example"></a>

```

CREATE TABLE test1 (a boolean, b text);
INSERT INTO test1 VALUES (TRUE, 'sic est');
INSERT INTO test1 VALUES (FALSE, 'non est');
SELECT * FROM test1;
 a |    b
---+---------
 t | sic est
 f | non est

SELECT * FROM test1 WHERE a;
 a |    b
---+---------
 t | sic est
```

<br>

在 SQL 查詢中撰寫布林常數時，關鍵字 `TRUE` 與 `FALSE` 是較建議（符合 SQL 標準）的方式。但你也可以依照[第 4.1.2.7 節](https://docs.postgresql.tw/19/the-sql-language/sql-syntax/sql-syntax-lexical/sql-syntax-constants.md#sql-syntax-constants-generic)所述的通用字串常數語法來使用字串表示法，例如 `'yes'::boolean`。

請注意，剖析器會自動理解 `TRUE` 與 `FALSE` 屬於 `boolean` 型別，但 `NULL` 並非如此，因為它可以是任何型別。因此在某些情境下，你可能必須明確地將 `NULL` 轉換為 `boolean`，例如 `NULL::boolean`。反過來說，在剖析器能夠推斷出字面值必定為 `boolean` 型別的情境下，字串常數形式的布林值就可以省略型別轉換。

***

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


---

# 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/datatype/datatype-boolean.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.
