Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a enum field with default value?

  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.

like image 682
RaviKiran Avatar asked Nov 09 '13 13:11

RaviKiran


People also ask

Can an enum have a default value?

The default value for an enum is zero. If an enum does not define an item with a value of zero, its default value will be zero.

How do I make enum default?

One consideration for the use case where a default enum is desired is to use a nullable variable. When a null is received, it can be translated in the correct part of the code into the default, and this default doesn't have to be known in the rest of the code (that just passes along a null).

What is default data type of enum?

The default value of an enumeration type E is the value produced by expression (E)0 , even if zero doesn't have the corresponding enum member. You use an enumeration type to represent a choice from a set of mutually exclusive values or a combination of choices.

What is the default value of enum in C++?

By default, the value of the first enumerator is zero if it is implicitly defined. The value of each subsequent implicitly-defined enumerator is the value of the previous enumerator + 1. (Optional) The name of a variable of the enumeration type.


1 Answers

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.

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.

like image 140
Sudarshan Kalebere Avatar answered Sep 16 '22 13:09

Sudarshan Kalebere