简体   繁体   中英

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. But I keep getting this error for ENUM() Below is the query for creating the table structure:

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. You can use CREATE TYPE syntax to create your enum and then use it in the schema to create table.

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

Followed by the CREATE TABLE statement,

 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.

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.

Postgres - like many other DBMS - is case sensitive when comparing strings. So with the above constraint you won't be able to store 'male' into the gender column.


Unrelated but: if you were assuming that varchar(255) has some magic performance benefits compared to eg varchar(300) then you are wrong. The maximum length of a varchar column does not influence the performance or the space requirements when storing the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM