postgresql:将所有条目乘以固定数

postgresql: multiply all entries by a fxed number

我有一个 postgresql 数据库和一个 table 这样的数据库:

Column         |              Type              |
---------------+--------------------------------+--
id             | bigint                         |
amount         | numeric(8,2)                   | 

更准确地说,我有:


id   | amount  |
-----+---------+-
 509 | -100.00 |
 517 |  -10.50 |
 518 |   -7.40 |

我想将 amount 的类型更改为 bigint。在此之前,我想将每个条目乘以 100。

我试着这样做: update entrees set amount = amount * 100;

但是我遇到了这些错误(我得到的是法语,翻译是我的:)

ERROR:  numerical field exceeds limits 
DETAIL : A field with precision 6 and scale 2 must be rounded to a value that is absolutely inferior to 10^4.

我的猜测是我需要在乘法之前将类型 numeric(6,2) 更改为其他类型。

这是真的吗?如何将此字段中的所有条目乘以 100?

我会这样做:

alter table table_name alter column amount type bigint;

更新table_name设置金额=(金额* 100);

alter table table_name alter column amount type numeric(8,2);

最安全的方法是:

  1. 再添加一个 bigint 类型的列,

  2. 用它来存储相乘后的值

  3. 检查新值是否是您需要的;

  4. 删除现有金额列

  5. 将新列重命名为金额

    改变TABLE主菜添加列amount_int BIGINT;

    更新主菜集amount_int=数量*100;

    ALTER TABLE 进入 DROP COLUMN 数量;

    ALTER TABLE entrees RENAME COLUMN amount_int TO amount;

您可以在单个 ALTER table:

中完成
alter table the_table
   alter amount type bigint using (amount * 100)::bigint;