PostgreSQL 将列从整数转换为文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8343286/
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:26:37  来源:igfitidea点击:

PostgreSQL Convert column from integer to text

sqldatabasepostgresql

提问by DaveB

I have a PostgreSQL (9.0) database with a column card_id which is currently of type integer

我有一个 PostgreSQL (9.0) 数据库,其中的 card_id 列当前为整数类型

I need to change this to type text

我需要将其更改为输入文本

What is the most best way to achieve this?

实现这一目标的最佳方法是什么?

The only solution I can find involves creating a temporary column, dropping the original then renaming, I thought they might be a better method??

我能找到的唯一解决方案是创建一个临时列,删除原始列然后重命名,我认为它们可能是更好的方法?

回答by Milen A. Radev

Have you tried what the fine manual suggests:

您是否尝试过精美手册的建议

ALTER TABLE table ALTER COLUMN anycol TYPE anytype;

Depending on the current and the new type you may need to add USING ...to this statement. But in your specific case that should not be necessary I believe.

根据当前和新类型,您可能需要添加USING ...到此语句中。但在你的具体情况下,我相信这不是必要的。

回答by Michael Krelin - hacker

ALTER TABLE table ALTER COLUMN card_id SET DATA TYPE text;