database Postgres 将列整数更改为布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1740303/
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
Postgres Alter Column Integer to Boolean
提问by samsina
I've a field that is INTEGER NOT NULL DEFAULT 0 and I need to change that to bool.
我有一个 INTEGER NOT NULL DEFAULT 0 字段,我需要将其更改为 bool。
This is what I am using:
这就是我正在使用的:
ALTER TABLE mytabe
ALTER mycolumn TYPE bool
USING
CASE
WHEN 0 THEN FALSE
ELSE TRUE
END;
But I am getting:
但我得到:
ERROR: argument of CASE/WHEN must be type boolean, not type integer
********** Error **********
ERROR: argument of CASE/WHEN must be type boolean, not type integer
SQL state: 42804
Any idea?
任何的想法?
Thanks.
谢谢。
回答by catchdave
Try this:
尝试这个:
ALTER TABLE mytabe ALTER COLUMN mycolumn DROP DEFAULT;
ALTER TABLE mytabe ALTER mycolumn TYPE bool USING CASE WHEN mycolumn=0 THEN FALSE ELSE TRUE END;
ALTER TABLE mytabe ALTER COLUMN mycolumn SET DEFAULT FALSE;
You need to remove the constraint first (as its not a boolean), and secondly your CASE
statement was syntactically wrong.
您需要首先删除约束(因为它不是布尔值),其次您的CASE
语句在语法上是错误的。
回答by Rahul Shinde
Postgres can automatically cast integer to boolean. The key phrase is
Postgres 可以自动将整数转换为布尔值。关键语句是
using some_col_name::boolean
-- here some_col_name is the column you want to do type change
Above Answer is correct that helped me Just one modification instead of case I used type casting
以上答案是正确的,对我有帮助 只是一个修改,而不是我使用类型转换的情况
ALTER TABLE mytabe ALTER COLUMN mycolumn DROP DEFAULT;
ALTER TABLE mytabe ALTER mycolumn TYPE bool USING mycolumn::boolean;
ALTER TABLE mytabe ALTER COLUMN mycolumn SET DEFAULT FALSE;