PostgreSQL:十进制/数字数据类型的数字字段溢出 - 为什么会出现此错误

PostgreSQL: Numeric field overflow on decimal / numeric Data Type - Why does it gives this Error

我创建了一个这样的table

CREATE TABLE foo (
    id serial CONSTRAINT id_pk PRIMARY KEY,
    bar varchar(256),
    test decimal(5,5)
);

然后我想填充 test

 INSERT INTO foo (test) VALUES (12345.12345)

这给了我

Numeric field overflow Detail: A field with precision 5, scale 5must round to an absolute value less than 1.

我不明白这个。我虽然如果我将列设置为 decimal(5,5),我 必须 在逗号左侧有五个数字,在逗号右侧有五个数字。我有号码 12345.12345.

为什么会给我这个错误?

您误解了指定小数的工作原理(我同意这有点令人困惑)。

Quote from the manual

The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point. The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point

所以第一个数字定义了位数,第二个定义了小数位数。

如果你想要小数点前5位和小数点后5位,你需要使用decimal(10,5)