PostgreSQL - ALTER 列数据类型从整数到整数数组

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

PostgreSQL - ALTER column data type from integer to integer array

postgresqltype-conversion

提问by srchalikonda

Kindly help to modify a column of type integer to integer array:

请帮助将整数类型的列修改为整数数组:

I had created a table with a column content_idof type integer. then I tried to change the content_id(integer)to integer[](integer array)but its showing error as displayed:

我创建了一个包含content_id整数类型列的表。然后我尝试将其更改content_id(integer)integer[](integer array)但其显示错误如下所示:

TestDatabase=# ALTER TABLE tbl_handset_content ALTER COLUMN content_id TYPE integer[];
ERROR:  column "content_id" cannot be cast to type "pg_catalog.int4[]"

Regards,

问候,

Sravan

斯拉文

回答by Wojtas

Try this (column test_id is of type INTEGER before alter takes place). PostgreSQL 8.4.

试试这个(在改变发生之前,列 test_id 是 INTEGER 类型)。PostgreSQL 8.4。

ALTER TABLE test.test_id
    ALTER COLUMN test_id TYPE INTEGER[]
    USING array[test_id]::INTEGER[];

回答by Fotoncito

This worked better for me!

这对我来说效果更好!

ALTER TABLE schema.table
    ALTER COLUMN column
    DROP DEFAULT;
ALTER TABLE schema.table
    ALTER COLUMN column TYPE INTEGER[]
    USING array[column]::INTEGER[];
ALTER TABLE schema.table
    ALTER COLUMN column SET DEFAULT '{}';