[英]Alter a table column into smallint column in postgres
我有一個名為update_profile的postgres表,其中有一列是表:
我想將此列更改為包含update_type_id值的smallint 。
最初我嘗試過:
ALTER TABLE update_profile ALTER COLUMN update_type TYPE SMALLINT USING update_type.update_type_id;
但是我有以下錯誤:表“update_type”缺少 FROM 子句條目
然后我嘗試了:
ALTER TABLE update_profile AS u ALTER COLUMN u.update_type TYPE SMALLINT USING u.update_type.update_type_id;
這是不允許的。
注意: update_type_id也是一個smallint
有沒有辦法做這個操作?
引用其他列時不要重復表名。 您也不能為表(或列)分配任何別名。
ALTER TABLE update_profile
ALTER COLUMN update_type TYPE SMALLINT
USING update_type_id;
這就是我最終做的:
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;
因為我沒有找到改變列update_type
的方法,所以我創建了一個名為update_type_id
的新列,傳遞了update_profile.update_type.update_type_id
的值,然后刪除了update_type
。
所以現在我在update_profile.update_type.update_type_id
中有了update_profile.update_type_id
的值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.