Why can i not get this database to work? - mysqli

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

Related

PostgreSQL gives syntax error at "foreign" or near

These are tables before referances
CREATE TABLE olap.time (
idtime SERIAL NOT NULL PRIMARY KEY,
year integer,
month integer,
week integer,
day integer
);
CREATE TABLE olap.addressees (
idaddressee integer PRIMARY KEY NOT NULL,
name varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(60) NOT NULL
);
CREATE TABLE olap.customers (
idcustomer varchar(10) SERIAL PRIMARY KEY autoincrement,
name varchar(40) NOT NULL,
city varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(40) NOT NULL,
email varchar(40),
phone varchar(16) NOT NULL,
regon char(9)
);
After creating this tables I want to create this table
CREATE TABLE olap.fact(
idtime integer NOT NULL,
idaddressee integer NOT NULL,
idcustomer varchar(10) NOT NULL,
idfact integer NOT NULL,
price numeric(7,2),
PRIMARY KEY (idtime, idaddressee, idcustomer),
FOREIGN KEY (idaddressee) REFERENCES olap.addressees(idaddressee),
FOREIGN KEY (idcustomer REFERENCES olap.customers(idcustomer),
FOREIGN KEY (idtime) REFERENCES time(idtime)
));
But I get error as
"ERROR: syntax error at or near "REFERENCES"
LINE 9: FOREIGN KEY (idcustomer REFERENCES olap.customers(idcustom..."
Thanks in advance
The idcustomer from olap.fact and idcustomer from olap.customers has different datatype SERIAL and Varchar(10),
I have corrected the datatypes and validated the code below
CREATE TABLE olap.time (
idtime SERIAL NOT NULL PRIMARY KEY,
year integer,
month integer,
week integer,
day integer
);
CREATE TABLE olap.addressees (
idaddressee integer PRIMARY KEY NOT NULL,
name varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(60) NOT NULL
);
CREATE TABLE olap.customers (
idcustomer varchar(10) PRIMARY KEY ,
name varchar(40) NOT NULL,
city varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(40) NOT NULL,
email varchar(40),
phone varchar(16) NOT NULL,
regon char(9)
);
CREATE TABLE olap.fact(
idtime integer NOT NULL,
idaddressee integer NOT NULL,
idcustomer varchar(10) NOT NULL,
idfact integer NOT NULL,
price numeric(7,2),
PRIMARY KEY (idtime, idaddressee, idcustomer),
FOREIGN KEY (idaddressee) REFERENCES olap.addressees(idaddressee),
FOREIGN KEY (idcustomer) REFERENCES olap.customers(idcustomer),
FOREIGN KEY (idtime) REFERENCES olap.time(idtime)
);

Postgres Primary Key as varchar is not working

First of all can I give Varchar for primary key? Here is my create table;
CREATE TABLE appinfo (
app_id VARCHAR (30) PRIMARY KEY,
app_secret VARCHAR ( 500 ) NOT NULL,
app_name VARCHAR ( 255 ) NOT NULL,
app_type VARCHAR ( 255 ) NOT NULL,
prog_lang VARCHAR ( 255 ) NULL,
description VARCHAR ( 255 ) NULL,
created_on TIMESTAMP NOT NULL DEFAULT NOW()
);
And I am trying to insert below value
INSERT INTO appinfo (app_id, app_secret, app_name, app_type)
VALUES ('ABC1', 'aaaaaaabbbbbbccccccccc', 'AccountInfoAPI', 'API');
but this shows below error;
ERROR: invalid input syntax for type integer: "ABC1"
LINE 2: VALUES ('ABC1', 'aaaaaaabbbbbbccccccccc', 'AccountInfoAPI...
I am very new to Postgres and I am not sure where is this Integer coming from. Or is that because Primary key is always Integer in Postgres?

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;

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

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.