简体   繁体   English

如何在Postgresql数据库中创建具有一列枚举数据类型的表?

[英]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


2. Because "Name" is a keyword in Postgres you also have to change the type name. 2.因为“名称”是Postgres中的关键字,所以您还必须更改类型名称。 Otherwise it will not work. 否则它将无法正常工作。


3. However: Your enum type has no values. 3.但是:您的枚举类型没有值。 So you are not able to insert any value. 因此,您将无法插入任何值。 You have to add some enum values: 您必须添加一些枚举值:

 CREATE TYPE name_type AS ENUM('name1', 'name2'); 


Final: 最后:

 CREATE TYPE name_type AS ENUM('name1', 'name2'); CREATE TABLE Employee2( ID integer, myname name_type ); 

demo: db<>fiddle 演示:db <> fiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从一个 PostgreSQL 数据库迁移到另一个(表/列名略有不同)? - How can I migrate data from one PostgreSQL database to another (with slightly different table/column names)? 如何在PostgreSQL中的视图中更改表列数据类型 - how to change a table column datatype in a view in postgresql PostgreSQL:如何使用ENUM数据类型? - Postgresql: How to use ENUM datatype? 如何在 Supabase 中创建枚举列? - How can I create an enum column in Supabase? 如何创建包含一组枚举值的PostgreSQL列? - How to create a PostgreSQL column that is a set of enum values? 如何在 Java 中创建 PostgreSQL 数据库? - How can I create a PostgreSQL database in Java? 如何使用 knex 迁移更改 postgresql 数据库中列的数据类型? - How to change datatype of a column in postgresql database using knex migration? 如何更改 PostgreSQL 数据库中表中身份类型主键列的当前值? - How can I change current value for identity type primary key column in table in PostgreSQL database? 如何截断(substring)PostgreSQL数据库中表的特定列的所有值? - How can I truncate (substring) all values for a particular column of a table in a PostgreSQL database? 我如何通过PostgreSQL 9.1的一个查询将TABLE的所有列从非null更改为null - How can i Alter TABLE all column from not null to null by one query of PostgreSQL 9.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM