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