

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://www.amazonaws.cn/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# 数字类型的示例
<a name="r_Examples_with_numeric_types201"></a>



## CREATE TABLE 语句
<a name="r_Examples_with_numeric_types201-create-table-statement"></a>

以下 CREATE TABLE 语句演示不同数字数据类型的声明：

```
create table film (
film_id integer,
language_id smallint,
original_language_id smallint,
rental_duration smallint default 3,
rental_rate numeric(4,2) default 4.99,
length smallint,
replacement_cost real default 25.00);
```

## 尝试插入超出范围的整数
<a name="r_Examples_with_numeric_types201-attempt-to-insert-an-integer-that-is-out-of-range"></a>

以下示例尝试将值 33000 插入到 SMALLINT 列中。

```
insert into film(language_id) values(33000);
```

SMALLINT 的范围为 -32768 至 \+32767，因此 Amazon Redshift 将返回错误。

```
An error occurred when executing the SQL command:
insert into film(language_id) values(33000)

ERROR: smallint out of range [SQL State=22003]
```

## 将小数值插入到整数列中
<a name="r_Examples_with_numeric_types201-insert-a-decimal-value-into-an-integer-column"></a>

以下示例将小数值插入到 INT 列中。

```
insert into film(language_id) values(1.5);
```

插入此值，但会将其四舍五入为整数值 2。

## 插入由于小数位数四舍五入而成功的小数
<a name="r_Examples_with_numeric_types201-insert-a-decimal-that-succeeds-because-its-scale-is-rounded"></a>

以下示例将具有更高精度的小数值插入列中。

```
insert into film(rental_rate) values(35.512);
```

在此示例中，值 `35.51` 将插入列中。

## 尝试插入超出范围的小数值
<a name="r_Examples_with_numeric_types201-attempt-to-insert-a-decimal-value-that-is-out-of-range"></a>

在此示例中，值 `350.10` 超出范围。DECIMAL 列中值的位数等于此列的精度减去其小数位数（对于 RENTAL\_RATE 列为 4 减去 2）。换言之，`DECIMAL(4,2)` 列所允许的范围为 `-99.99` 到 `99.99`。

```
insert into film(rental_rate) values (350.10);
ERROR:  numeric field overflow
DETAIL:  The absolute value is greater than or equal to 10^2 for field with precision 4, scale 2.
```

## 将精度可变的值插入到 REAL 列中
<a name="r_Examples_with_numeric_types201-insert-variable-precision-values-into-a-real-column"></a>

以下示例将精度可变的值插入到 REAL 列中。

```
insert into film(replacement_cost) values(1999999.99);

insert into film(replacement_cost) values(1999.99);

select replacement_cost from film;

+------------------+
| replacement_cost |
+------------------+
| 2000000          |
| 1999.99          |
+------------------+
```

值 `1999999.99` 将转换为 `2000000` 以满足 `REAL` 列的精度要求。值 `1999.99` 将按原样加载。