Reputation: 437
How can I create a table with one column of enum datatype in PostgreSQL database?
Table name: Employee
Columns:
ID: Integer
Name: ENUM
Below is the query but not sure it is correct or not.
CREATE TYPE Name AS ENUM();
CREATE TABLE IF NOT EXISTS Employee(
ID integer NOT NULL,
Name DEFAULT NULL,
CONSTRAINT "Employee_pkey" PRIMARY KEY (id)
);
Can someone please help.
Upvotes: 8
Views: 22003
Reputation: 186
Here you got a simple example, consider to add a name to your enum column at Employee Table, and add some values to your enum.
CREATE TYPE NameEnum AS ENUM('Jony','Bala','Mark');
CREATE TABLE IF NOT EXISTS Employee(
ID integer NOT NULL,
name NameEnum DEFAULT NULL,
CONSTRAINT "Employee_pkey" PRIMARY KEY (id)
);
Insert into Employee(ID,name)
Values(1, (SELECT enum_first(NULL::NameEnum)))
Select * from Employee
Output:
Upvotes: 6
Reputation: 23756
1. In the line
Name DEFAULT NULL,
you either forgot the name of the column or defining the columns as enum type:
myname Name DEFAULT NULL, -- add column name
or
Name Name DEFAULT NULL, -- add enum type
CREATE TYPE name_type AS ENUM('name1', 'name2');
CREATE TYPE name_type AS ENUM('name1', 'name2');
CREATE TABLE Employee2(
ID integer,
myname name_type
);
Upvotes: 11