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

时间:2016-07-20 07:09:55

标签: postgresql

我是Postgresql的新手,我试图创建这个表实际上只是跟着一个类似的mysql表。但我继续为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
);

任何帮助将不胜感激。感谢

2 个答案:

答案 0 :(得分:4)

ENUM是用户定义的数据类型。您可以使用CREATE TYPE语法创建枚举,然后在架构中使用它来创建表。

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

后面是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
 );

有关枚举创建和使用的更多信息,请参阅postgresql文档https://www.postgresql.org/docs/current/static/datatype-enum.html

答案 1 :(得分:1)

你不需要这个枚举(我个人认为永远不需要枚举 - 但这不是主题)。

您应该将其实现为检查约束:

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))
)

但是,对于is_delete最好是合适的boolean列 - 那么您也不需要该列的检查约束。

Postgres - 与许多其他DBMS一样 - 在比较字符串时区分大小写。因此,使用上述约束,您将无法将'male'存储到gender列中。

不相关但是:如果您假设varchar(255)具有一些神奇的性能优势,例如varchar(300)那你错了。存储值时,varchar列的最大长度不会影响性能或空间要求。