简体   繁体   English

将表列更改为 postgres 中的 smallint 列

[英]Alter a table column into smallint column in postgres

I have a postgres table called update_profile with a column that is a table:我有一个名为update_profilepostgres表,其中有一列是表: 在此处输入图像描述

And I want to alter this column to a smallint containing the value of update_type_id .我想将此列更改为包含update_type_id值的smallint

Initially I tried:最初我尝试过:

ALTER TABLE update_profile ALTER COLUMN update_type TYPE SMALLINT USING update_type.update_type_id;

But I had the following error: missing FROM-clause entry for table "update_type"但是我有以下错误:表“update_type”缺少 FROM 子句条目

Then I tried:然后我尝试了:

ALTER TABLE update_profile AS u ALTER COLUMN u.update_type TYPE SMALLINT USING u.update_type.update_type_id;

Which is not allowed.这是不允许的。

Note: update_type_id is also a smallint注意: update_type_id也是一个smallint

Is there a way to do this operation?有没有办法做这个操作?

Don't repeat the table name when you reference the other column.引用其他列时不要重复表名。 You can't assign any alias for the table (or column) either.您也不能为表(或列)分配任何别名。

ALTER TABLE update_profile 
   ALTER COLUMN update_type TYPE SMALLINT
   USING update_type_id;

This is what I ended up doing:这就是我最终做的:

ALTER TABLE update_profile ADD COLUMN update_type_id SMALLINT;

UPDATE update_profile up SET update_type_id =
(
    SELECT ut.update_type_id
    FROM n3rgy_update_type ut
    WHERE ut = up.update_type
)
WHERE up.update_type IS NOT NULL;

ALTER TABLE update_profile DROP COLUMN update_type;

Because I didn't find a way to alter the column update_type , I created a new column called update_type_id , passed the values of update_profile.update_type.update_type_id , and then dropped update_type .因为我没有找到改变列update_type的方法,所以我创建了一个名为update_type_id的新列,传递了update_profile.update_type.update_type_id的值,然后删除了update_type

So now I have the values of update_profile.update_type.update_type_id in update_profile.update_type_id所以现在我在update_profile.update_type.update_type_id中有了update_profile.update_type_id的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM