简体   繁体   English

PostgreSQL:如何使用ENUM数据类型?

[英]Postgresql: How to use ENUM datatype?

I am new to Postgresql, I am trying to create this table actually just following a similar mysql table. 我是Postgresql的新手,实际上是在遵循类似的mysql表之后尝试创建此表。 But I keep getting this error for ENUM() Below is the query for creating the table structure: 但是我仍然收到ENUM()的错误,以下是用于创建表结构的查询:

CREATE TABLE IF NOT EXISTS gkb_users (
id bigint NOT NULL,
userid character varying(50) NOT NULL DEFAULT '',
password character varying(50) NOT NULL DEFAULT '',
firstname text NOT NULL,
middlename text NOT NULL,
lastname text NOT NULL,
email character varying(100) NOT NULL DEFAULT '',
gender enum('Male','Female') NOT NULL,
dob date NOT NULL,
mobile character varying(10) NOT NULL DEFAULT '',
telephone character varying(15) NOT NULL DEFAULT '',
city character varying(50) NOT NULL DEFAULT '',
address text NOT NULL,
shippingaddress text NOT NULL,
PIN character varying(255) NOT NULL,
shipping_PIN character varying(255) NOT NULL,
area character varying(255) NOT NULL,
shipping_area character varying(255) NOT NULL,
previouscart text NOT NULL,
updatedon timestamp(0) NOT NULL,
is_deleted enum('0','1') NOT NULL
);

Any help will be greatly appreciated. 任何帮助将不胜感激。 Thanks 谢谢

ENUM is a user-defined datatype. ENUM是用户定义的数据类型。 You can use CREATE TYPE syntax to create your enum and then use it in the schema to create table. 您可以使用CREATE TYPE语法来创建您的枚举,然后在架构中使用它来创建表。

CREATE TYPE your_enum2 AS ENUM('0','1');
CREATE TYPE your_enum1 AS ENUM('male','female');

Followed by the CREATE TABLE statement, 接着是CREATE TABLE语句,

 CREATE TABLE IF NOT EXISTS gkb_users (
 id bigint NOT NULL,
 userid character varying(50) NOT NULL DEFAULT '',
 password character varying(50) NOT NULL DEFAULT '',
 firstname text NOT NULL,
 middlename text NOT NULL,
 lastname text NOT NULL,
 email character varying(100) NOT NULL DEFAULT '',
 gender your_enum1 NOT NULL,
 dob date NOT NULL,
 mobile character varying(10) NOT NULL DEFAULT '',
 telephone character varying(15) NOT NULL DEFAULT '',
 city character varying(50) NOT NULL DEFAULT '',
 address text NOT NULL,
 shippingaddress text NOT NULL,
 PIN character varying(255) NOT NULL,
 shipping_PIN character varying(255) NOT NULL,
 area character varying(255) NOT NULL,
 shipping_area character varying(255) NOT NULL,
 previouscart text NOT NULL,
 updatedon timestamp(0) NOT NULL,
 is_deleted your_enum2 NOT NULL
 );

Refer postgresql docs https://www.postgresql.org/docs/current/static/datatype-enum.html for more information on enum creation and usage. 有关枚举创建和使用的更多信息,请参考postgresql文档https://www.postgresql.org/docs/current/static/datatype-enum.html

You don't need an enum for this (and I personally think one never needs an enum - but that's off topic). 您不需要为此枚举(我个人认为一个人不需要枚举-但这没什么意思)。

You should either implement this as a check constraint: 您应该将其实现为检查约束:

CREATE TABLE IF NOT EXISTS gkb_users 
(
  id                bigint NOT NULL,
  userid            varchar(50) NOT NULL DEFAULT '',
  password          varchar(50) NOT NULL DEFAULT '',
  firstname         text NOT NULL,
  middlename        text NOT NULL,
  lastname          text NOT NULL,
  email             varchar(100) NOT NULL DEFAULT '',
  gender            text NOT NULL,
  dob               date NOT NULL,
  mobile            varchar(10) NOT NULL DEFAULT '',
  telephone         varchar(15) NOT NULL DEFAULT '',
  city              varchar(50) NOT NULL DEFAULT '',
  address           text NOT NULL,
  shippingaddress   text NOT NULL,
  pin               varchar(255) NOT NULL,
  shipping_pin      varchar(255) NOT NULL,
  area              varchar(255) NOT NULL,
  shipping_area     varchar(255) NOT NULL,
  previouscart      text NOT NULL,
  updatedon         timestamp(0) NOT NULL,
  is_deleted        integer NOT NULL,
  constraint check_gender check (gender in ('Male', 'Female')), 
  constraint check_deleted flag check (is_deleted in (0,1))
)

However, for is_delete should better be a proper boolean column - then you also don't need a check constraint for that column. 但是,因为is_delete最好是一个正确的boolean列-那么您也不需要该列的检查约束。

Postgres - like many other DBMS - is case sensitive when comparing strings. 与其他许多DBMS一样,Postgres在比较字符串时区分大小写。 So with the above constraint you won't be able to store 'male' into the gender column. 因此,根据上述限制,您将无法在gender列中存储'male'


Unrelated but: if you were assuming that varchar(255) has some magic performance benefits compared to eg varchar(300) then you are wrong. 无关,但是:如果您假设varchar(255)varchar(300)相比具有一些神奇的性能优势,那么您是错误的。 The maximum length of a varchar column does not influence the performance or the space requirements when storing the values. 存储值时,varchar列的最大长度不影响性能或空间要求。

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

相关问题 如何在Postgresql中使用LO数据类型? - How to use LO datatype with postgresql? 如何在Postgresql数据库中创建具有一列枚举数据类型的表? - How can i create a table with one column of enum datatype in postgresql database? 如何在postgresql“json”数据类型中存储日期以用于plv8? - How to store a date in postgresql “json” datatype for use with plv8? 如何告诉PostgreSQL不要验证数据类型 - How to tell PostgreSQL not to verify datatype 如何在 PostgreSQL SELECT 语句中使用自定义类型枚举? - How would I use a custom Type Enum in a PostgreSQL SELECT statement? 如何通过 Kotlin 暴露 ORM 使用 Postgresql 枚举类型? - How to use Postgresql enum type via Kotlin Exposed ORM? PostgreSQL:哪种数据类型更适合这种情况? - PostgreSQL: Which datatype is better to use for this situation? 如何使用to_sql方法使用postgresql和pandas用点数据类型的列填充表? - How to use to_sql method to fill table with column with point datatype using postgresql and pandas? 如何在Postgresql中将Earth数据类型转换为JSON - How to cast earth datatype to json in Postgresql 如何在postgresql中删除货币数据类型的精度部分? - How to remove precision part of money datatype in postgresql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM