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

src*_*nda 10 postgresql type-conversion

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

我创建了一个包含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[]"
Run Code Online (Sandbox Code Playgroud)

问候,

Sravan

小智 24

试试这个(在test发生之前,列test_id是INTEGER类型).PostgreSQL 8.4.

ALTER TABLE test.test_id
    ALTER COLUMN test_id TYPE INTEGER[]
    USING array[test_id]::INTEGER[];
Run Code Online (Sandbox Code Playgroud)

  • 使用第一个元素迁移回来就像:```ALTER TABLE test.test_id ALTER COLUMN test_id TYPE INTEGER USING test_id [1] :: INTEGER;```注意,默认情况下,数组是1索引的. (2认同)

小智 5

这对我来说效果更好!

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 '{}';
Run Code Online (Sandbox Code Playgroud)