如何在 postgresql 中更改类型和删除值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25889398/
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
How to alter type and remove value in postgresql
提问by Alex
I found how to add value to the TYPE
. But how can I remove value from it?
For example I have TYPE
with enum
values ('A','B','C')
. How to remove 'C'
?
我找到了如何为TYPE
. 但是我怎样才能从中删除价值呢?
例如,我有TYPE
与enum
价值观('A','B','C')
。如何去除'C'
?
回答by snaiffer
To remove value ('val1') from enum ('enum_test') you can use:
要从枚举 ('enum_test') 中删除值 ('val1'),您可以使用:
DELETE FROM pg_enum
WHERE enumlabel = 'val1'
AND enumtypid = (
SELECT oid FROM pg_type WHERE typname = 'enum_test'
)
回答by leszy77
"DELETE FROM pg_enum" answer works.
“从 pg_enum 中删除”答案有效。
However, one has to be aware that it can be dangerous, because if the deleted enum value exists somewhere in some tables in the database, the delete still works without complaining but all the records with the deleted value become corrupt. E.g. any SELECT query that has any such record in its result set, crashes.
但是,必须意识到这可能是危险的,因为如果已删除的枚举值存在于数据库中某些表的某处,则删除仍然可以正常工作,但具有已删除值的所有记录都会损坏。例如,任何在其结果集中有任何此类记录的 SELECT 查询都会崩溃。