[英]How can i create a table with one column of enum datatype in postgresql database?
How can I create a table with one column of enum datatype in PostgreSQL database? 如何在PostgreSQL数据库中创建具有一列enum数据类型的表?
Table name: Employee
表名:
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. 有人可以帮忙吗?
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. 在这里,您有一个简单的示例,考虑在Employee Table的enum列中添加一个名称,并在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:
1. In the line 1.在行中
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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.