Postgresql: How to use ENUM datatype? - postgresql

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 e.g. 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.

Related

Installation failed to create the core tables | Installing Revive Adserver 5.2.0

I am installing revive ad server on Ubuntu 20.4 which is having MySQL Version 8.0.23. The Log I got from file /var/debug.log is
[Last executed query: CREATE TABLE `bv_banners` (`bannerid` MEDIUMINT(9) AUTO_INCREMENT NOT NULL, `campaignid` MEDIUMINT(9) DEFAULT 0 NOT NULL, `contenttype` ENUM('gif','jpeg','png','html','swf','dcr','rpm','mov','txt') DEFAULT 'gif' NOT NULL, `pluginversion` MEDIUMINT(9) DEFAULT 0 NOT NULL, `storagetype` ENUM('sql','web','url','html','network','txt') DEFAULT 'sql' NOT NULL, `filename` VARCHAR(255) DEFAULT '' NOT NULL, `imageurl` VARCHAR(255) DEFAULT '' NOT NULL, `htmltemplate` MEDIUMTEXT NOT NULL, `htmlcache` MEDIUMTEXT NOT NULL, `width` SMALLINT(6) DEFAULT 0 NOT NULL, `height` SMALLINT(6) DEFAULT 0 NOT NULL, `weight` TINYINT(4) DEFAULT 1 NOT NULL, `seq` TINYINT(4) DEFAULT 0 NOT NULL, `target` VARCHAR(16) DEFAULT '' NOT NULL, `url` TEXT NOT NULL, `alt` VARCHAR(255) DEFAULT '' NOT NULL, `statustext` VARCHAR(255) DEFAULT '' NOT NULL, `bannertext` TEXT NOT NULL, `description` VARCHAR(255) DEFAULT '' NOT NULL, `adserver` VARCHAR(255) DEFAULT '' NOT NULL, `block` INT(11) DEFAULT 0 NOT NULL, `capping` INT(11) DEFAULT 0 NOT NULL, `session_capping` INT(11) DEFAULT 0 NOT NULL, `compiledlimitation` TEXT NOT NULL, `acl_plugins` TEXT DEFAULT NULL, `append` TEXT NOT NULL, `bannertype` TINYINT(4) DEFAULT 0 NOT NULL, `alt_filename` VARCHAR(255) DEFAULT '' NOT NULL, `alt_imageurl` VARCHAR(255) DEFAULT '' NOT NULL, `alt_contenttype` ENUM('gif','jpeg','png') DEFAULT 'gif' NOT NULL, `comments` TEXT DEFAULT NULL, `updated` DATETIME NOT NULL, `acls_updated` DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL, `keyword` VARCHAR(255) DEFAULT '' NOT NULL, `transparent` TINYINT(1) DEFAULT 0 NOT NULL, `parameters` TEXT DEFAULT NULL, `status` INT(11) DEFAULT 0 NOT NULL, `ext_bannertype` VARCHAR(255) DEFAULT NULL, `prepend` TEXT NOT NULL, `iframe_friendly` TINYINT(1) DEFAULT 1 NOT NULL, PRIMARY KEY (bannerid)) ENGINE = INNODB]
[Native message: Invalid default value for 'acls_updated']
I got to know adserver is not officially supporting mysql version 8. But some people are able to run fine. I want to know how.
Here is the refrence I got
https://github.com/revive-adserver/revive-adserver/issues/1048
I want to run this.
Thanks in advance.
set sql_mode='40'' to sql_mode=''
in 5 files;
lib/OA/DB.php
lib/OA/DaL/Delivery/mysql.php
lib/OA/Dal/Delivery/mysqli.php
lib/OA/Upgrade/DB_Upgrade.php
lib/OA/Upgrade/Upgrade.php
and use table type INNODB
This trick works for me. I am calling this as a trick because it is not officially supported.
I got solution from github.
Here is the refrence
https://github.com/revive-adserver/revive-adserver/issues/1048

How to change the migration and not destroy anything?

I have a user table and I should remove the NOT NULL restrictions from the firstname, username and lastname fields, do I understand correctly that nothing can be changed directly, do I need to add a new sql file and with the command? How can I remove NOT NULL and break nothing? everything breaks down
CREATE TABLE "user" (
id SERIAL UNIQUE PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
last_password_reset TIMESTAMP NOT NULL DEFAULT NOW(),
roles JSONB NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
date_created TIMESTAMP NOT NULL DEFAULT NOW(),
date_updated TIMESTAMP NOT NULL DEFAULT NOW()
);
ALTER TABLE user ALTER COLUMN username DROP NOT NULL;

Postgres SQL constraint a character type

I have a table definition in Postgres. I would like to add a constraint to a column that is of Character datatype to have only 3 allowed values:
CREATE TABLE my_table
(
id character varying(255) NOT NULL,
uid character varying(255) NOT NULL,
my_text text NOT NULL,
is_enabled boolean NOT NULL
);
So I want the my_text column to contain only 'A', 'B' or 'C' as values.
Where can I find some documentation on this?
Use a check constraint:
CREATE TABLE my_table
(
id character varying(255) NOT NULL,
uid character varying(255) NOT NULL,
my_text text NOT NULL,
is_enabled boolean NOT NULL,
constraint check_allowed check (my_text in ('A', 'B', 'C'))
);
More details in the manual: http://www.postgresql.org/docs/current/static/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS
If you want to be able to add caracters without modyfying the condition:
CREATE TABLE my_ref
(
my_value varying(1) PRIMARY KEY
)
INSERT INTO my_ref VALUES( 'A' );
INSERT INTO my_ref VALUES( 'B' );
INSERT INTO my_ref VALUES( 'C' );
CREATE TABLE my_table
(
id character varying(255) NOT NULL,
uid character varying(255) NOT NULL,
my_text text NOT NULL,
is_enabled boolean NOT NULL,
constraint check_allowed FOREIGN KEY( my_text ) REFERENCES my_ref( my_value )
);
You won't be able to add values in my_text that aren't in my_ref table.

Will My Table Be Acceptable For PostgreSQL?

I want to create a table in a PostgreSQL database to store user data (Account info, contact info, geographical info). I have typed out the following SQL and I am wondering is it acceptable for PostgreSQL (in terms of best practices, data types, and lengths)?
CREATE TABLE users (
userID SERIAL,
username VARCHAR(255) NOT NULL,
password VARCHAR(60) NOT NULL,
email VARCHAR(255) NOT NULL,
active VARCHAR(255) NOT NULL,
lenderAcct BOOLEAN NOT NULL DEFAULT FALSE,
resetToken VARCHAR(255) DEFAULT NULL,
resetComplete VARCHAR(3) DEFAULT 'No',
CONSTRAINT users_pk PRIMARY KEY (userID),
firstName VARCHAR(20) NOT NULL,
middleName VARCHAR(20),
lastName VARCHAR(20) NOT NULL,
primaryPhone VARCHAR(50) NOT NULL,
primaryPhoneExt VARCHAR(10),
altPhone VARCHAR(50),
altPhoneExt VARCHAR(10),
fax VARCHAR(50),
legalAddress1 VARCHAR(25) NOT NULL,
legalAddress2 VARCHAR(25),
legalCity VARCHAR(25) NOT NULL,
legalState VARCHAR(25) NOT NULL,
legalZip VARCHAR(16) NOT NULL,
legalCountry VARCHAR(25) NOT NULL,
mailAddress1 VARCHAR(25) NOT NULL,
mailAddress2 VARCHAR(25),
mailCity VARCHAR(25) NOT NULL,
mailState VARCHAR(25) NOT NULL,
mailZip VARCHAR(16) NOT NULL,
mailCountry VARCHAR(25) NOT NULL
);
The PostgreSQL side of the table looks ok. You could consider changing the varchar(x) types to text (check out this answer). The PRIMARY KEY can be introduced as userID SERIAL PRIMARY KEY, ... although this has no effect on the table structure.
Best practises cannot be commented on more without knowing your full table structure but those considerations are mostly not PostgreSQL specific anyway.

Why can i not get this database to work?

Good day I am having problems getting this database to work using email instead of a username however it does not seem to work. a solution with an explanation would be highly appreciated. Thank you in advance
$tbl_users = "CREATE TABLE IF NOT EXISTS users (
id INT (11) NOT NULL AUTO_INCREMENT,
firstname VARCHAR (16) NOT NULL,
lastname VARCHAR (16) NOT NULL,
username VARCHAR (16) NOT NULL,
age VARCHAR (16) NOT NULL,
gender ENUM ('m','f') NOT NULL,
email VARCHAR (255) NOT NULL,
website VARCHAR (255) NULL,
country VARCHAR (255) NULL,
userlevel ENUM ('a','b','c','d') NOT NULL DEFAULT 'a',
avatar VARCHAR (255) NULL,
ip VARCHAR (255) NOT NULL,
signup DATETIME NOT NULL,
lastlogin DATETIME NOT NULL,
notescheck DATETIME NOT NULL,
activated ENUM ('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (id),
UNIQUE KEY email
)";
$query = mysqli_query ($db_conx, $tbl_users);
if ($query === TRUE) {
echo "<h3>user table created</h3>";
} else {
echo "<h3>user table not created</h3>";
}
I think you need UNIQUE KEY (email) with ()- see documentation here and here