Definition of PostgreSQL enum
PostgreSQL enum is the data type that was used in PostgreSQL to stored same type of values in column field, we can store same type of values using enum. For defining enum data type we need to create the type of enum first to use it into the table, we can define multiple values in enum type to use the same into the table. If we need same column values in table, same time we have using enum.
Syntax:
Below is the syntax of enum in PostgreSQL:
- Create enum type
Create type name_of_enum_type (value_of_enum_type1, value_of_enum_type2, value_of_enum_type3, …, value_of_enum_typeN);
- Create table using enum type
Create table name_of_table (name_of_column1 data_type, name_of_column2 enum_type, name_of_column3 data_type, …, name_of_columnN data_type);
- Insert a value of enum data type column
Insert into name_of_table (name_of_column1, name_of_enum_type_column2, name_of_column3, …, name_of_columnN) values (value1, value_of_enum_type, value2, value3, …., ValueN);
- Alter enum type
Alter type name_of_enum_type add value ‘value_of_enum_type’ AFTER ‘value_of_enum_type’;
Below is the parameter description syntax of enum type in PostgreSQL.
- Create type – This is defined as create enum data type using create type in PostgreSQL.
- Name of enum type –This is define as create enum type and use this in table at the time of table creation. We can use this data type on a column at the time of table creation.
- Name of table –This is defined as name of table on which column we have defining enum data type.
- Value of enum type 1 to value of enum type N –This is defined as set of value which was we have used in enum data type. We can define multiple value in enum type, after defining the same we have using this into the table at the time of insertion.
- Create table – This is defined as create a table by using the enum type, we can define enum type to the column in PostgreSQL.
- Name of column 1 to name of column N –This is defined as name of column which was we have used at the time of table creation. We have also define enum type to the column.
- Value 1 to value N –This is defined as column field value which was used at the time of insertion into the table.
- Alter type –We can alter enum type after creation, we can modify the enum type by using the alter type command. We can add a new value into the enum data set to use the same into the table.
- Add value –This is defined as add a new value to the enum type by using the alter type command.
How enum works in PostgreSQL?
- Below is the working of enum type in PostgreSQL.
- If we have use same type of value into the column, same time we have used enum type on the table column.
- For using enum type we need to create it first, without creating we cannot use into the table.
- Below example shows that we need to create enum type first,without creating we cannot use into the table.
Code:
CREATE TABLE enum_info (enum_name text, enum_method enum_type, enum_value text);
CREATE TYPE enum_type AS ENUM ('mail', 'text_sms', 'Phone_no');
CREATE TABLE enum_info (enum_name text, enum_method enum_type, enum_value text);
\d+ enum_info;
- In first example, we have creating enum type table without creating but it will show the error that enum type is not exist.
- In second example we have created enum type as enum_type. In third example we have use this on enum_method column, same time this will not issue error because we have created enum type before we have use into the table.
- After using enum type on table column we cannot use other value which was not contain into the enum type data set.
- Below example shows that we cannot use other value which was not contain into the enum type data set.
Code:
insert into enum_info values ('ABC', 'Email', 'PQR');
insert into enum_info values ('ABC', 'mail', 'PQR');
select * from enum_info;
- In first example, we have use-value as email in enum type column value but it will issue error invalid input value of enum because we have used value is not stored into the enum data set.
- In second example we have use-value as mail it will not issue error because mail value is available into the enum data set.
Examples
Below is the example of enum type in PostgreSQL.
Example #1: Create enum type
Below example shows that create enum type. We have creating enum type name as enum_method_test. Also, we have added value as test1, test2, and test3.
Code:
CREATE TYPE enum_method_test AS ENUM ('test1', 'test2', 'test3');
Example #2: Create a table by using enum type
The below example shows that create a table by using enum type. We have creating table name as enum_info_test. Also, we have added enum type on column name as enum_method.
Code:
CREATE TABLE enum_info_test (enum_name text, enum_method enum_method_test, enum_value text);
\d+ enum_info_test;
Example #3: Insert value of enum data type column
Below example shows that insert value into the enum data type column.
Code:
insert into enum_info_test values ('ABC', 'test1', 'enum_data_type');
insert into enum_info_test values ('PQR', 'test2', 'enum_data_type');
insert into enum_info_test values ('PQR', 'test3', 'enum_data_type');
insert into enum_info_test values ('PQR', 'test4', 'enum_data_type');
select * from enum_info_test;
Example #4: Alter enum type
Code:
Below example show that alter enum type. We have adding new value as test4 into the enum_method_test type.
ALTER TYPE enum_method_test ADD VALUE 'test4' AFTER 'test3';
Example #5: Drop enum type
Below example shows that drop enum type.
Code:
drop type enum_method_test;
Recommended Articles
This is a guide to PostgreSQL enum. Here we discuss the definition, How enum works in PostgreSQL? and examples with code implementation. You may also have a look at the following articles to learn more –