Updated May 17, 2023
Introduction to PostgreSQL enum
PostgreSQL enum is the data type that was used in PostgreSQL to store the same type of values in the column field, we can store the 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 the enum type to use the same into the table. If we need the same column values in the table, same time, we are using enum.
Syntax:
Below is the syntax of the enum in PostgreSQL:
1. 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);
2. Create a table using the 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);
3. Insert a value of the 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);
4. 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 the enum type in PostgreSQL:
- Create type: This is defined as creating enum data using create type in PostgreSQL.
- Name of enum type: This is defined 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 the table’s name on which column we are defining the enum data type.
- Value of enum type 1 to enum type N: This is defined as a set of values we have used in the enum data type. We can define multiple values in enum type, after defining the same, we are using this into the table at the time of insertion.
- Create table: To create a table using an enum type in PostgreSQL, you can define the enum type for a column. This involves assigning the enum type to the desired column during the table creation process.
- Name of column 1 to the name of column N: This is defined as the name of the column which we have used at the time of table creation. We have also defined enum type to the column.
- Value 1 to value N: This is defined as the 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: To add a new value to the enum type, you can use the ALTER TYPE command. This command allows you to incorporate an additional value into the existing enum type.
How enum Works in PostgreSQL?
Below is the working of enum type in PostgreSQL:
- If we have used the same type of value into the column, at the same time, we have used the 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 it, 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;
- As we created the enum type table in the first example without first constructing it, the error message “enum type does not exist” appeared.
- In the second example, we have created an enum type as enum_type. In the third example, we used this on enum_method column, same time, this will not issue an error because we have created enum type before we have used into the table.
- After using the enum type on the table column, we cannot use other value which does not contain into the enum type data set.
- Below example shows that we cannot use other value which does 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 the first example, we have use-value as email in the enum type column value, but it will issue error invalid input value of the enum because we have used a value is not stored into the enum data set.
- In the second example, using the value “mail” will not result in an error since the “mail” value is present in the enum dataset.
Examples of PostgreSQL enum
Given below are the examples of enum type in PostgreSQL:
Example #1
Create enum type.
Below example shows that create an enum type. We are creating an 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 an enum type.
The below example shows that create a table by using an enum type. We are creating a table name as enum_info_test. Also, we have added an enum type on the 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 the value of the enum data type column.
Below example shows that insert a 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.
Below example show that alter enum type. We are adding a new value as test4 into the enum_method_test type.
Code:
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
We hope that this EDUCBA information on the “PostgreSQL enum” was beneficial to you. You can view EDUCBA’s recommended articles for more information.