将 PostgreSQL 列从整数更改为小数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6485440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:05:39  来源:igfitidea点击:

Alter PostgreSQL column from integer to decimal

postgresql

提问by Oli

Thanks to a last minute client request a integer field in our database now needs to be a decimal, to two points.A value of 23should become 23.00.

多亏了最后一分钟的客户请求,我们数据库中的整数字段现在需要是一个小数,要两个点。值23应该变成23.00

Is there a nice way I can convert the table and cast the data across?

有没有一种很好的方法可以转换表格并转换数据?

I'll freely admit, I haven't done anything like this with PostgreSQL before so please be gentle with me.

我会坦率地承认,我之前没有用 PostgreSQL 做过这样的事情,所以请对我温柔一点。

回答by Szymon Lipiński

Something like this should work:

这样的事情应该工作:

alter table t alter column c type decimal(10,2);

Edit:

编辑:

As @Oli stated in the comments; the first number is the entire length of the number (excluding the point) so the maxval for (10,2)would be 99999999.99

正如@Oli 在评论中所说;第一个数字是数字的整个长度(不包括点),以便为MAXVAL (10,2)。将99999999.99