> ## Documentation Index
> Fetch the complete documentation index at: https://villagesql.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# MySQL 的 vsql_currency 扩展

> vsql_currency 扩展为 MySQL 添加 currency 列类型，它用一个字节保存 164 个 ISO 4217 代码中的任何一个，输入不区分大小写。

货币代码列通常是一个 `CHAR(3)`，它接受任意三个字母，包括那些不是
货币的字母组合。`vsql_currency` 添加了一个 `currency` 类型，它用一个
字节保存 pg-currency 支持的 164 个 ISO 4217 代码，接受任意大小写的
输入，并以规范的大写形式显示。

| | |
| - | - |
| **维护者** | VillageSQL |
| **源代码和完整文档** | [villagesql/vsql-currency](https://github.com/villagesql/vsql-currency) |
| **许可证** | GPL-2.0 |
| **编写语言** | Rust |

## 安装

`vsql_currency` 未随服务器捆绑，因此请先从存储库构建它，并把生成的
`vsql_currency.veb` 复制到 `veb_dir` 系统变量所指定的目录中。
然后安装它：

```sql theme={null}
INSTALL EXTENSION vsql_currency;
```

确认它已存在：

```sql theme={null}
SELECT EXTENSION_NAME
FROM INFORMATION_SCHEMA.EXTENSIONS
WHERE EXTENSION_NAME = 'vsql_currency';
```

```
+----------------+
| EXTENSION_NAME |
+----------------+
| vsql_currency  |
+----------------+
```

构建说明在[存储库](https://github.com/villagesql/vsql-currency)中。

## 新增内容

### currency 类型

```sql theme={null}
CREATE TABLE prices (
    sku    VARCHAR(8) PRIMARY KEY,
    amount DECIMAL(10,2),
    unit   currency
);
```

### 函数

| 函数 | 返回 | 作用 |
| - | - | - |
| `currency_count()` | 整数 | 扩展认识多少个代码 |
| `is_currency(code)` | 整数 | 字符串是有效的 ISO 4217 代码时为 1，否则为 0 |
| `supported_currencies(prefix)` | 文本 | 以 JSON 数组表示的代码列表，按前缀筛选 |

## 示例

以任意大小写写入代码，读回时为规范形式并按顺序排列：

```sql theme={null}
INSERT INTO prices VALUES
    ('A-100', 19.99, 'usd'),
    ('A-101', 24.50, 'EUR'),
    ('A-102', 3100,  'jpy');

SELECT sku, amount, unit FROM prices ORDER BY unit;
```

```
+-------+---------+------+
| sku   | amount  | unit |
+-------+---------+------+
| A-101 |   24.50 | EUR  |
| A-102 | 3100.00 | JPY  |
| A-100 |   19.99 | USD  |
+-------+---------+------+
```

在信任某个代码之前先检查它，并列出您关心的代码：

```sql theme={null}
SELECT currency_count() AS codes, is_currency('XYZ') AS xyz, is_currency('GBP') AS gbp;
```

```
+-------+------+------+
| codes | xyz  | gbp  |
+-------+------+------+
|   164 |    0 |    1 |
+-------+------+------+
```

```sql theme={null}
SELECT supported_currencies('C') AS starting_with_c;
```

```
+---------------------------------------------------------------------+
| starting_with_c                                                     |
+---------------------------------------------------------------------+
| ["CAD","CDF","CHF","CLP","CNY","COP","CRC","CUC","CUP","CVE","CZK"] |
+---------------------------------------------------------------------+
```

## 另请参阅

* [选择数据类型](https://villagesql.com/docs/en/guides/choosing-data-types.md) — 更窄的类型何时值得使用
* [安装扩展](https://villagesql.com/docs/zh/extensions/install.md) — `INSTALL EXTENSION` 如何工作，以及服务器在哪里查找包
* [可用扩展](https://villagesql.com/docs/zh/extensions/catalog) — 完整目录
* [villagesql/vsql-currency](https://github.com/villagesql/vsql-currency) — 源代码、构建说明和已知限制


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.