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

# F.20. isn — 國際標準號碼的資料型別（ISBN、EAN、UPC 等） \#

### F.20. isn — 國際標準號碼的資料型別（ISBN、EAN、UPC 等） [#](#isn) <a href="#isn" id="isn"></a>

[F.20.1. 資料型別](https://docs.postgresql.tw/19/appendixes/contrib/isn/isn-data-types.md#isn-data-types)

[F.20.2. 型別轉換](#isn-casts)

[F.20.3. 函式與運算子](https://docs.postgresql.tw/19/appendixes/contrib/isn/isn-funcs-ops.md#isn-funcs-ops)

[F.20.4. 組態參數](#isn-configuration-parameters)

[F.20.5. 範例](#isn-examples)

[F.20.6. 參考資料](#isn-bibliography)

[F.20.7. 作者](#isn-author)

`isn` 模組為下列國際產品編號標準提供資料型別：EAN13、UPC、ISBN（書籍）、ISMN（音樂）與 ISSN（連續出版品）。輸入時，會依硬式編碼的前綴清單驗證號碼；輸出時也會使用此前綴清單將號碼加上連字號。由於不時會指派新的前綴，前綴清單可能已過時。希望此模組的未來版本能從一個或多個使用者可依需求輕易更新的資料表取得前綴清單；但目前只能修改原始碼並重新編譯以更新清單。或者，此模組未來版本可能移除前綴驗證與連字號支援。

此模組被視為「受信任」，亦即具有目前資料庫 `CREATE` 權限的非超級使用者可以安裝它。

#### F.20.1. 資料型別 [#](#isn-data-types) <a href="#isn-data-types" id="isn-data-types"></a>

完整內容請見 [F.20.1. 資料型別](https://docs.postgresql.tw/19/appendixes/contrib/isn/isn-data-types.md#isn-data-types)。

#### F.20.2. 型別轉換 [#](#isn-casts) <a href="#isn-casts" id="isn-casts"></a>

`isn` 模組提供下列成對的型別轉換：

* ISBN13 <=> EAN13
* ISMN13 <=> EAN13
* ISSN13 <=> EAN13
* ISBN <=> EAN13
* ISMN <=> EAN13
* ISSN <=> EAN13
* UPC <=> EAN13
* ISBN <=> ISBN13
* ISMN <=> ISMN13
* ISSN <=> ISSN13

從 `EAN13` 轉換為另一型別時，執行期間會檢查值是否在另一型別的定義域內；若否，會引發錯誤。其他轉換只是重新標示，必定成功。

#### F.20.3. 函式與運算子 [#](#isn-funcs-ops) <a href="#isn-funcs-ops" id="isn-funcs-ops"></a>

完整內容請見 [F.20.3. 函式與運算子](https://docs.postgresql.tw/19/appendixes/contrib/isn/isn-funcs-ops.md#isn-funcs-ops)。

#### F.20.4. 組態參數 [#](#isn-configuration-parameters) <a href="#isn-configuration-parameters" id="isn-configuration-parameters"></a>

`isn.weak` (`boolean`)[#](#ISN-CONFIGURATION-PARAMETERS-WEAK) : `isn.weak` 啟用弱輸入模式，即使 ISN 輸入值的檢查碼錯誤也可接受。預設值為 `false`，會拒絕無效的檢查碼。

為何要使用弱模式？可能是您有大量 ISBN 號碼，其中有些因不明原因具有錯誤的檢查碼（例如從印刷清單掃描時 OCR 辨識錯誤，或手動擷取號碼時出錯）。您可能想清理這些資料，但仍希望將所有號碼保留在資料庫中，並可能使用外部工具找出資料庫中的無效號碼，以便更容易驗證資訊並確認有效性；例如，您可能想選取資料表中所有無效號碼。

以弱模式將無效號碼插入資料表時，會以修正後的檢查碼插入該號碼，但顯示時會在末端加上驚嘆號（`!`），例如 `0-11-000322-5!`。可使用 `is_valid` 函式檢查此無效標記，並使用 `make_valid` 函式清除它。

即使未啟用弱模式，也可在號碼末端附加 `!` 字元，強制插入標記為無效的號碼。

另一項特殊功能是，輸入時可在檢查碼位置寫入 `?`，系統會自動插入正確的檢查碼。

#### F.20.5. 範例 [#](#isn-examples) <a href="#isn-examples" id="isn-examples"></a>

```

--Using the types directly:
SELECT isbn('978-0-393-04002-9');
SELECT isbn13('0901690546');
SELECT issn('1436-4522');

--Casting types:
-- note that you can only cast from ean13 to another type when the
-- number would be valid in the realm of the target type;
-- thus, the following will NOT work: select isbn(ean13('0220356483481'));
-- but these will:
SELECT upc(ean13('0220356483481'));
SELECT ean13(upc('220356483481'));

--Create a table with a single column to hold ISBN numbers:
CREATE TABLE test (id isbn);
INSERT INTO test VALUES('9780393040029');

--Automatically calculate check digits (observe the '?'):
INSERT INTO test VALUES('220500896?');
INSERT INTO test VALUES('978055215372?');

SELECT issn('3251231?');
SELECT ismn('979047213542?');

--Using the weak mode:
SET isn.weak TO true;
INSERT INTO test VALUES('978-0-11-000533-4');
INSERT INTO test VALUES('9780141219307');
INSERT INTO test VALUES('2-205-00876-X');
SET isn.weak TO false;

SELECT id FROM test WHERE NOT is_valid(id);
UPDATE test SET id = make_valid(id) WHERE id = '2-205-00876-X!';

SELECT * FROM test;

SELECT isbn13(id) FROM test;
```

#### F.20.6. 參考資料 [#](#isn-bibliography) <a href="#isn-bibliography" id="isn-bibliography"></a>

實作此模組的資訊蒐集自數個網站，包括：

* <https://www.isbn-international.org/>
* <https://www.issn.org/>
* <https://www.ismn-international.org/>
* <https://www.wikipedia.org/>

用於連字號的前綴也彙整自：

* <https://www.gs1.org/standards/id-keys>
* <https://en.wikipedia.org/wiki/List_of_ISBN_registration_groups>
* <https://www.isbn-international.org/content/isbn-users-manual/29>
* <https://en.wikipedia.org/wiki/International_Standard_Music_Number>
* <https://www.ismn-international.org/ranges/tools>

建立演算法時格外謹慎，並依官方 ISBN、ISMN、ISSN 使用者手冊中建議的演算法仔細驗證。

#### F.20.7. 作者 [#](#isn-author) <a href="#isn-author" id="isn-author"></a>

Germán Méndez Bravo (Kronuz), 2004–2006

此模組受到 Garrett A. Wollman 的 `isbn_issn` 程式碼啟發。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/isn.html)


---

# 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/appendixes/contrib/isn.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.
