我的postgres数据库模式的表中有一个数值列,它的精度为n。
我需要将此列更新为较低的精度,将其称为m。
所以m < n。
当我运行该命令时:
ALTER TABLE my_table
ALTER COLUMN my_column
TYPE numeric(m, 2)我得到以下错误:numeric field overflow
我理解这是因为我在表中存储的值的精度大于m (我希望新精度达到的值)。有没有什么方法可以告诉postgres更改精度并忽略丢失的精度?例如。
如果n= 20和m= 15,并且我在my_column中的值是99999999999999999,如果我没有弄错的话,这个精度是17,我可以告诉postgres忽略丢失的精度,并在我修改表/列时将新值保存为999999999999999吗?
发布于 2020-03-07 03:16:40
您可以使用using为转换提供一个表达式:
alter table t alter column col
type numeric(2) using (case when col >= 100 then 99 else col end);Here是一个db<>fiddle。
https://stackoverflow.com/questions/60570258
复制相似问题