postgresql 如何创建具有默认值的枚举字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19876764/
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 create a enum field with default value?
提问by RaviKiran
types = { # add your custom types here
'attendance': ('Notconfirmed','Coming', 'Notcoming', 'Maycome',),
}
CREATE TYPE attendance AS ENUM types;
The above query creates enum type attendance with enumlabels mentioned in types. How to create a type with default label? In this case the I want to create attendance type with default value Notconfirmed.
上面的查询使用类型中提到的枚举标签创建枚举类型出勤。如何创建带有默认标签的类型?在这种情况下,我想创建默认值 Notconfirmed 的出勤类型。
回答by Sudarshan Kalebere
I was trying the same as you, and I got answer in stackoverflow only, It is possible to create ENUM with default value. Here is what I got for you.
我和你一样在尝试,我只在 stackoverflow 中得到了答案,可以使用默认值创建 ENUM。这是我为你准备的。
CREATE TYPE status AS ENUM ('Notconfirmed','Coming', 'Notcoming', 'Maycome');
CREATE TABLE t (
id serial,
s status default 'Notconfirmed' -- <==== default value
);
INSERT INTO t(id) VALUES (default) RETURNING *;
This worked for me like a charm.
这对我来说就像一种魅力。
回答by Norman Edance
In addition to Sudarshan's words...
除了苏达山的话……
In case someone needs an example in different schema:
如果有人需要不同模式的示例:
CREATE TABLE schema_name.table_name ( --
id serial,
s schema_name.type_name default 'Notconfirmed'::schema_name.type_name
);
回答by ferdousulhaque
I am not sure why we need to have below query in our table. I have tried without this line still works.
我不知道为什么我们需要在我们的表中有以下查询。我试过没有这条线仍然有效。
INSERT INTO t(id) VALUES (default) RETURNING *;