Syntax Error in MySQL - Same syntax worked in earlier table - mysql

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

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.

mysql error 1072

COMMIT WORK
;
DROP DATABASE IF EXISTS Campaign_Database;
CREATE DATABASE Campaign_Database;
USE Campaign_Database;
DROP TABLE IF EXISTS CONTRIBUTOR
;
DROP TABLE IF EXISTS CAMPAIGN
;
DROP TABLE IF EXISTS PLEDGE
;
CREATE TABLE CONTRIBUTOR
( CONTRIBUTOR_ID VARCHAR(50) primary key,
Address char(50) not null,
City char(50) not null,
State char(2) not null,
Zip INTEGER(6) not null,
Home_Phone char (15)
)ENGINE = InnoDB;
;
CREATE TABLE CAMPAIGN
( CAMPAIGN_ID VARCHAR(50) primary key,
Chair_First_Name char (50) not null,
Chair_Last_Name char (50) not null,
Launch_Date char (11) not null,
End_Date char (11) not null,
Fundraising_Goal INTEGER NOT NULL
)ENGINE = InnoDB;
;
CREATE table PLEDGE
(
FOREIGN KEY (CONTRIBUTOR_ID) REFERENCES CONTRIBUTOR (CONTRIBUTOR_ID),
FOREIGN KEY (CAMPAIGN_ID) REFERENCES CAMPAIGN (CAMPAIGN_ID),
Date_Pledged char(11) not null primary key,
Amount INTEGER NOT NULL,
PAID char(1) not null
CHECK (PAID in ('Y','N')),
Payment_Date char(11)
)ENGINE = InnoDB;
;
COMMIT WORK
;
This current is the mysql coding I am using for a finals assignment.
I have tried several methods but was not successful in creating the two foreign keys within the 'pledge' table.
I would be appreciated if I can at least know what went wrong. Thank you.
You need a CONTRIBUTOR_ID, CAMPAIGN_ID fields in the PLEDGE table.
CREATE table PLEDGE
(
CONTRIBUTOR_ID VARCHAR(50),
CAMPAIGN_ID VARCHAR(50),
FOREIGN KEY (CONTRIBUTOR_ID) REFERENCES CONTRIBUTOR (CONTRIBUTOR_ID),
FOREIGN KEY (CAMPAIGN_ID) REFERENCES CAMPAIGN (CAMPAIGN_ID),
Date_Pledged char(11) not null primary key,
Amount INTEGER NOT NULL,
PAID char(1) not null
CHECK (PAID in ('Y','N')),
Payment_Date char(11)
)ENGINE = InnoDB;
;
In this statement
FOREIGN KEY (CONTRIBUTOR_ID) REFERENCES CONTRIBUTOR (CONTRIBUTOR_ID),
The first CONTRIBUTOR_ID refers to the table where the contraint is defined and the second refers to the table where it is pointing to.

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

"Key column doesn't exist in table" when trying to define FOREIGN KEY

I'm trying to setup a MySQL-Script that creates 5 tables. In three tables, there is a FOREIGN KEY, and for all three of them the same error appears:
Error Code: 1072. Key column ... doesn't exist in table
whereas ... are Gebaeude, Dept_Name and Mat_Nr
Here's the script
use cs261_24;
drop table if exists Professor;
drop table if exists Departement;
drop table if exists Gebaeude;
drop table if exists Student;
drop table if exists Pruefung;
CREATE TABLE Gebaeude (
Gebaeude VARCHAR(20) NOT NULL PRIMARY KEY,
Hauswart VARCHAR(20) NOT NULL,
Adresse VARCHAR(20) NOT NULL
)engine = innodb;
CREATE TABLE Professor (
P_ID INTEGER PRIMARY KEY CHECK (P_ID > 0),
P_Name VARCHAR(20) NOT NULL,
Dept_Name VARCHAR(20) NOT NULL,
Raum INTEGER UNIQUE CHECK (Raum > 0),
Tel INTEGER(10) UNIQUE CHECK (Tel > 210000000),
FOREIGN KEY (Gebaeude) REFERENCES Gebaeude (Gebaeude)
)engine = innodb;
CREATE TABLE Departement (
Dept_Name VARCHAR(20) NOT NULL PRIMARY KEY,
Vorsteher VARCHAR(20) NOT NULL
)engine = innodb;
CREATE TABLE Student (
Mat_Nr INTEGER(8) PRIMARY KEY CHECK (Mat_Nr > 0),
S_Name VARCHAR(20) NOT NULL,
Semester INTEGER CHECK(Semester > 0),
FOREIGN KEY (Dept_Name) REFERENCES Departement (Dept_Name)
)engine = innodb;
CREATE TABLE Pruefung (
Pr_ID INTEGER PRIMARY KEY CHECK(Pr_ID > 0),
Fach VARCHAR(20) NOT NULL,
Pruefer VARCHAR(20) NOT NULL,
Note FLOAT CHECK (Note >= 1 AND Note <= 6),
FOREIGN KEY (Mat_Nr) REFERENCES Student (Mat_Nr)
)engine = innodb;
Why? I work with MySQL Workbench, and I clearly see the created tables, plus the specific columns are marked as primary keys!
You are doing it wrong, take a look to this example
http://www.sqlfiddle.com/#!2/a86cf
your FK line should be more like this:
FOREIGN KEY (field_that_will_be_Fk) REFERENCES Table_to_reference (field_to_reference)
I.E=
CREATE TABLE Gebaeude (
Gebaeude VARCHAR(20) NOT NULL PRIMARY KEY,
Hauswart VARCHAR(20) NOT NULL,
Adresse VARCHAR(20) NOT NULL
)engine = innodb;
CREATE TABLE Professor (
Gebaeude_FK varchar(20) NOT NULL,
P_ID INTEGER PRIMARY KEY CHECK (P_ID > 0),
P_Name VARCHAR(20) NOT NULL,
Dept_Name VARCHAR(20) NOT NULL,
Raum INTEGER UNIQUE CHECK (Raum > 0),
Tel INTEGER(10) UNIQUE CHECK (Tel > 210000000),
FOREIGN KEY (Gebaeude_FK) REFERENCES Gebaeude (Gebaeude)
)engine = innodb;
You have to create the column in each of the tables before you can constrain them as a foreign key.
See this link for reference: Link