Database creation and double checking for mistakes - mysql

I created a database in one of my project but I'm not sure if I still doing it right. I'm still learning sql and I stuggle in some of the tables I make. The operations are the following.
I can add officer with it's assign division.
I have to check every division what their plan and their corresponding items with milestone(months) each year.
I can also check how many items per milestone(months) & vice Versa.
Tables are officer, division, plan, items, milestone, when I try to create this table in http://sqlfiddle.com I got an error of Can't create database 'db_9_85e4c'; database exists when I try to alter and create a foreign key in officer & division table.
Here's my tables:
Officer Table
create table officer
(
id int not null auto_increment
primary key,
name varchar(100) not null,
s_position varchar(50) null,
f_position varchar(100) not null,
constraint officer_name_uindex
unique (name),
constraint officer_position_uindex
unique (f_position),
constraint officer_division_division_id_fk
foreign key (id) references test.division (division_id)
on update cascade on delete cascade
)
;
Division Table
create table division
(
division_id int not null
primary key,
acronym varchar(50) not null,
sequence_id int not null,
decription varchar(50) not null,
constraint division_sequesnce_id_uindex
unique (sequence_id),
constraint division_acronym_uindex
unique (acronym),
constraint division_decription_uindex
unique (decription),
constraint division_ppmp_ppmp_id_fk
foreign key (division_id) references test.ppmp (ppmp_id)
on update cascade on delete cascade
)
;
Plan Table
create table ppmp
(
ppmp_id int not null auto_increment
primary key,
year varchar(50) not null
)
;
Items Table
create table items
(
id int not null auto_increment
primary key,
description varchar(100) not null,
unit varchar(50) not null,
quantity int not null,
budget double not null,
mode varchar(50) not null,
constraint items_milestone_id_fk
foreign key (id) references test.milestone (id)
on update cascade on delete cascade,
constraint items_ppmp_ppmp_id_fk
foreign key (id) references test.ppmp (ppmp_id)
on update cascade on delete cascade
)
;
Milestone Table
create table milestone
(
id int not null auto_increment
primary key,
january int null,
february int null,
march int null,
april int null,
may int null,
june int null,
july int null,
august int null,
september int null,
october int null,
november int null,
decemeber int null
)
;

Create the schema with the tables in this order
Milestone
ppmp
items
division
officer
the problem is happening because Officer have a foreing key to division_id but that table and column does not exists yet.

Related

MYSQL Foreign key incorrectly formed

Have an error where my first foreign key constraint is incorrectly formed. I'm waiting to hear back from my lecturer if she can spot the issue but thought I'd ask here
My SQL query is as follows
CREATE DATABASE rugby;
USE rugby;
CREATE TABLE address(
address_ID INT AUTO_INCREMENT NOT NULL,
address_name VARCHAR(50) NOT NULL,
PRIMARY KEY (address_ID)
) ENGINE=INNODB;
CREATE TABLE team(
team_ID INT AUTO_INCREMENT NOT NULL,
team_Name VARCHAR(25) NOT NULL,
team_Year INT NOT NULL,
PRIMARY KEY(team_ID)
) ENGINE=INNODB;
CREATE TABLE person(
person_ID INT AUTO_INCREMENT NOT NULL,
name VARCHAR(30) NOT NULL,
phone INT NOT NULL,
address_ID INT NOT NULL,
email VARCHAR(50),
photo BLOB,
PRIMARY KEY(person_ID),
FOREIGN KEY(address_ID) REFERENCES address(address_ID) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=INNODB;
I have followed how we were taught but can't spot the issue.
Any help appreciated
In table person you have defined address_ID as NOT NULL, but
when you define the FOREIGN KEY you set:
ON DELETE SET NULL
which contradicts the NOT NULL definition.

MySQL Error (HY000): Cannot add foreign key constraint

I'm trying to create a MySQL schema for a simple online shop. My existing tables are as follows:
user — user info
address — address info for users
product — product info
purchase — a shopping chart created and purchased by a user
I've created these tables using the following SQL code:
CREATE TABLE user(
user_id INT AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
email VARCHAR(40) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
registered BOOLEAN NOT NULL,
phone VARCHAR(12),
PRIMARY KEY(user_id)
);
CREATE TABLE product(
product_id INT AUTO_INCREMENT,
name VARCHAR(40) NOT NULL UNIQUE,
price DECIMAL(5,2) NOT NULL,
description VARCHAR(100) UNIQUE,
PRIMARY KEY(product_id)
);
CREATE TABLE address(
user_id INT,
address_id INT,
city VARCHAR(15) NOT NULL,
district VARCHAR(20) NOT NULL,
details VARCHAR(50) NOT NULL,
is_default BOOLEAN NOT NULL,
FOREIGN KEY(user_id) REFERENCES user(user_id) ON DELETE CASCADE,
PRIMARY KEY(user_id, address_id)
);
CREATE TABLE purchase(
purchase_id INT AUTO_INCREMENT,
user_id INT,
total_price DECIMAL(8,2) NOT NULL,
state ENUM('placed', 'paid', 'shipped', 'received', 'completed', 'cancelled') DEFAULT 'placed',
user_name VARCHAR(40) NOT NULL,
full_address VARCHAR(85) NOT NULL,
FOREIGN KEY(user_id) REFERENCES user(user_id) ON DELETE SET NULL,
PRIMARY KEY(purchase_id)
);
Now I'm trying to create a final table to store individual items in a shopping chart like this:
CREATE TABLE purchase_item(
purchase_id INT,
product_id INT,
product_name VARCHAR(40) NOT NULL,
amount DECIMAL(4,2) NOT NULL,
price DECIMAL(8,2) NOT NULL,
FOREIGN KEY(purchase_id) REFERENCES purchase(purchase_id) ON DELETE CASCADE,
FOREIGN KEY(product_id) REFERENCES product(product_id) ON DELETE SET NULL,
PRIMARY KEY(purchase_id, product_id)
);
But I get the following error:
ERROR 1215 (HY000): Cannot add foreign key constraint
I don't any errors if I update the foreign key for products as follows:
FOREIGN KEY(product_id) REFERENCES product(product_id) ON DELETE CASCADE,
I want the purchase_item to be deleted if the corresponding purchase gets deleted, but not when the corresponding product gets deleted.
https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html says:
If you specify a SET NULL action, make sure that you have not declared the columns in the child table as NOT NULL.
But your purchase_item.product_id column is part of that table's primary key, which implicitly makes the column NOT NULL.
You cannot use the SET NULL action for the primary key on that column.

Syntax Error in MySQL - Same syntax worked in earlier table

I'm working on building a small database and am running into a syntax error on these two lines in my last table.
They're foreign keys and those lines didn't produce any errors in the tables where they originally appeared.
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
DONUT_ID VARCHAR(10) NOT NULL UNIQUE,
These two lines are the PK and FK in the table, and I think this is where I'm running into the problem because I used the same syntax as I did in earlier tables that aren't generating any errors.
Here is the full code for four tables:
CREATE TABLE CUSTOMER (
CUST_ID INT NOT NULL AUTO_INCREMENT UNIQUE,
CUST_LNAME VARCHAR(25) NOT NULL,
CUST_FNAME VARCHAR(25) NOT NULL,
CUST_INITIAL CHAR(1),
CUST_STREET_NO VARCHAR(6),
CUST_STREET_NAME VARCHAR(25),
CUST_APT_NO VARCHAR(10),
CUST_CITY VARCHAR(25),
CUST_STATE CHAR(2),
CUST_ZIP_CODE CHAR(5),
CUST_HOME_AC CHAR(3),
CUST_HOME_PHONE CHAR(8),
PRIMARY KEY (CUST_ID)
)ENGINE = InnoDB;
CREATE TABLE INVOICE (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_ID INTEGER NOT NULL,
INV_DATE DATE NOT NULL,
SPECIAL_HANDLING VARCHAR(35),
PRIMARY KEY (INVOICE_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER(CUST_ID) ON UPDATE CASCADE
)ENGINE = InnoDB;
CREATE TABLE PRODUCT (
DONUT_ID VARCHAR(10) NOT NULL UNIQUE,
DONUT_NAME VARCHAR(25) NOT NULL,
DONUT_DESC VARCHAR(35) NOT NULL,
DONUT_PRICE DECIMAL(13,2) NOT NULL,
PRIMARY KEY (DONUT_ID)
)ENGINE = InnoDB;
CREATE TABLE INVOICE LINE ITEM (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
DONUT_ID VARCHAR(10) NOT NULL UNIQUE,
DONUT_QTY INTEGER NOT NULL,
PRIMARY KEY (INVOICE_ID, DONUT_ID),
FOREIGN KEY (INVOICE_ID) REFERENCES INVOICE(INVOICE_ID) ON UPDATE CASCADE,
FOREIGN KEY (DONUT_ID) REFERENCES PRODUCT(DONUT_ID) ON UPDATE CASCADE
)ENGINE = InnoDB;
The fourth CREATE TABLE statement will fail because of the spaces in the table name:
CREATE TABLE INVOICE LINE ITEM ...
You should use a different name, without spaces. For example you could replace the spaces with underscores and use snake-case:
CREATE TABLE INVOICE_LINE_ITEM ...

Child Table variable allows NULL but the ON DELETE SET NULL still isn't working?

When I try to set the Foreign Key to ON DELETE SET NULL it won't create the table (ERROR 1005 (HY000):). If I change it to ON DELETE CASCADE it works. So I'm fairly certain that's the issue.
I have a table called ORDERHASSHIPPINGADDRESS that maintains the many-to-many relationship between CUSTORDER table and ADDRESS table. When a customer deletes an order this can cascade through and delete the record in ORDERHASSHIPINGADDRESS, but when an address is deleted it shouldn't delete the record in ORDERHASSHIPINGADDRESS - then it should just be set to NULL until it can be updated to something else. So I made sure the child table variable "addrID" allows NULL. I made sure InnoDB allows this. What else am I missing? Thank you in advance for your help.
CREATE TABLE USER (
id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY,
userName VARCHAR(20) NOT NULL UNIQUE,
email VARCHAR(25) NOT NULL UNIQUE,
firstName VARCHAR(15) NOT NULL,
lastName VARCHAR(20) NOT NULL,
salt CHAR(10) NOT NULL,
HASH CHAR(40)
)ENGINE=InnoDB;
CREATE TABLE ADDRESS (
id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY,
addName VARCHAR(25) NOT NULL UNIQUE,
streetAddress VARCHAR(50) NOT NULL,
city VARCHAR (25) NOT NULL,
state VARCHAR(2) NOT NULL,
zip CHAR(5) NOT NULL,
phone VARCHAR(10),
addrUserID INT UNSIGNED NOT NULL,
FOREIGN KEY (addrUserID) REFERENCES USER(id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB;
CREATE TABLE FUNDS (
id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY,
cardNumber VARCHAR(19) NOT NULL UNIQUE,
sudoName VARCHAR(25),
expDate DATE NOT NULL,
securityCode VARCHAR(5) NOT NULL UNIQUE,
billAddrID INT UNSIGNED,
FOREIGN KEY (billAddrID) REFERENCES ADDRESS(id) ON DELETE SET NULL ON UPDATE CASCADE
)ENGINE=InnoDB;
CREATE TABLE CUSTORDER (
id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY,
invoiceDate DATE NOT NULL,
orderUserID INT UNSIGNED,
orderFundsID INT UNSIGNED,
FOREIGN KEY (orderUserID) REFERENCES USER(id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (orderFundsID) REFERENCES FUNDS(id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB;
CREATE TABLE ORDERHASSHIPPINGADDRESS (
orderID INT UNSIGNED NOT NULL,
addrID INT UNSIGNED,
PRIMARY KEY (orderID, addrID),
FOREIGN KEY (orderID) REFERENCES CUSTORDER(id) on DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (addrID) REFERENCES ADDRESS(id) on DELETE SET NULL on UPDATE CASCADE
)ENGINE=InnoDB;
MySQL won't allow the SET NULL because I set the variable addrID as one of the PRIMARY KEYS so it cannot be null.

MySQL Table order is correct, data types match, so why do I get ERROR 1005 (HY000)

I have several tables and several of them reference each other to create many-to-many relationships. I realize data types must match and table order matters when creating the foreign keys. I think I have all that accounted for. So why am I getting the ERROR 1005 (HY000) in the following:
CREATE TABLE USER (
id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY,
userName VARCHAR(20) NOT NULL UNIQUE,
email VARCHAR(25) NOT NULL UNIQUE,
firstName VARCHAR(15) NOT NULL,
lastName VARCHAR(20) NOT NULL,
salt CHAR(10) NOT NULL,
HASH CHAR(40)
)ENGINE=InnoDB;
CREATE TABLE PRODUCT (
id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY,
prodName VARCHAR(50) NOT NULL UNIQUE,
price DECIMAL(6,2),
availability BOOLEAN,
description VARCHAR(150) NOT NULL,
size VARCHAR(20),
weight VARCHAR(10)
)ENGINE=InnoDB;
CREATE TABLE USERENTERSPRODUCT (
userID INT UNSIGNED NOT NULL,
prodID INT UNSIGNED NOT NULL,
PRIMARY KEY (userID, prodID),
FOREIGN KEY (userID) REFERENCES USER(id) ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (prodID) REFERENCES PRODUCT(id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB;
CREATE TABLE USERENTERSPRODUCT (
userID INT UNSIGNED NOT NULL,
prodID INT UNSIGNED NOT NULL,
PRIMARY KEY (userID, prodID),
FOREIGN KEY (userID) REFERENCES USER(id) ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (prodID) REFERENCES PRODUCT(id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB;
You are specifying ON DELETE SET NULL here, but at the same time both columns have NOT NULL specified as well … that obviously can’t work.
So either remove the NOT NULL from the column definition, or change ON DELETE SET NULL to whatever matches you needs best. (Normally that should be CASCADE as well, that makes the most sense.)