What is wrong with my foreign key in mySQL? - mysql

Whenever I try to run this script in mySQL I get an error that reads:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIG' at line 7
The code for the table it is referencing is on top and the code for the table with the failing foreign key constraint is below:
CREATE TABLE CATEGORY(
Category_Name varchar(35) NOT NULL,
ShippingPerPound DECIMAL(4,2),
OffersAllowed ENUM('y', 'n'),
CONSTRAINT CATEGORY_pk PRIMARY KEY(Category_Name)
);
CREATE TABLE ITEM(
Item_Number int UNSIGNED AUTO_INCREMENT,
Item_Name varchar(35) NOT NULL,
Description varchar(255),
Model varchar(50) NOT NULL,
Price DECIMAL(8,2) NOT NULL,
Category_Name,
CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIGN KEY (Category_Name) REFERENCES CATEGORY(Category_Name) ON UPDATE CASCADE
);

Your Category_Name declaration in yourITEM` table is incomplete:
Try:
CREATE TABLE ITEM(
Item_Number int UNSIGNED AUTO_INCREMENT,
Item_Name varchar(35) NOT NULL,
Description varchar(255),
Model varchar(50) NOT NULL,
Price DECIMAL(8,2) NOT NULL,
Category_Name varchar(35) NOT NULL,
CONSTRAINT ITEM_pk PRIMARY KEY(Item_Number),
CONSTRAINT ITEM_CATEGORY_fk FOREIGN KEY (Category_Name) REFERENCES CATEGORY(Category_Name) ON UPDATE CASCADE
);

Related

Mysql statement is having syntax issue

I am running below the MYSQL query and getting this error. Can someone help me here? I am using xampp to run MySQL and executing the statement
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NOT NULL,
DeliveryMode varchar NOT NULL,
Qty int NOT NULL,
Spicy ' at line 6
Below is my create table query
CREATE TABLE Bag (
BagId int NOT NULL AUTO_INCREMENT,
DishName varchar NOT NULL,
DeliveryMode varchar NOT NULL,
Qty int NOT NULL DEFAULT 1,
Spicy varchar(20) NULL,
SpecialInstructionsFood varchar(250),
BagStatus varchar(100) NOT NULL,
DateCreated datetime DEFAULT current_timestamp(),
DateUpdated datetime DEFAULT current_timestamp(),
FOREIGN KEY(DishName) REFERENCES Dish(DishName),
CONSTRAINT FK_DishName_Bag FOREIGN KEY(DishName)
REFERENCES Dish(DishName),
FOREIGN KEY(DeliveryMode) REFERENCES DeliveryModeType(Name),
CONSTRAINT FK_DeliveryModeType_Bag FOREIGN KEY(DeliveryMode)
REFERENCES DeliveryModeType(Name),
FOREIGN KEY(Spicy) REFERENCES SpicyType(Name),
CONSTRAINT FK_SpicyType_Bag FOREIGN KEY(Spicy)
REFERENCES SpicyType(Name),
FOREIGN KEY(BagStatus) REFERENCES BagStatusType(Name),
CONSTRAINT FK_BagStatusType_Bag FOREIGN KEY(BagStatus)
REFERENCES BagStatusType(Name),
PRIMARY KEY (BagId)
);
When you define a varchar in mysql you must provide the max length that it supports.
So modify your columns(DishName & DeliveryMode) which uses varchar as datatype to something like varchar(50) whichever suits them.
DishName VARCHAR(50) NOT NULL,
DeliveryMode VARCHAR(20) NOT NULL
CREATE TABLE Bag (
BagId int(11) NOT NULL AUTO_INCREMENT,
DishName varchar(100) NOT NULL,
DeliveryMode varchar(40) NOT NULL,
Qty int(11) NOT NULL DEFAULT 1,
Spicy varchar(20),
SpecialInstructionsFood varchar(250),
BagStatus varchar(100) NOT NULL,
DateCreated datetime DEFAULT current_timestamp(),
DateUpdated datetime DEFAULT current_timestamp(),
FOREIGN KEY(DishName) REFERENCES Dish(DishName),
CONSTRAINT FK_DishName_Bag FOREIGN KEY(DishName)
REFERENCES Dish(DishName),
FOREIGN KEY(DeliveryMode) REFERENCES DeliveryModeType(Name),
CONSTRAINT FK_DeliveryModeType_Bag FOREIGN KEY(DeliveryMode)
REFERENCES DeliveryModeType(Name),
FOREIGN KEY(Spicy) REFERENCES SpicyType(Name),
CONSTRAINT FK_SpicyType_Bag FOREIGN KEY(Spicy)
REFERENCES SpicyType(Name),
FOREIGN KEY(BagStatus) REFERENCES BagStatusType(Name),
CONSTRAINT FK_BagStatusType_Bag FOREIGN KEY(BagStatus)
REFERENCES BagStatusType(Name),
PRIMARY KEY (BagId)
);

Error when uploading table to server

I am having trouble adding 3 tables to my database (MINOR_DEGREES, STUDENT, STUDENT_RECORD).
Can anyone assist?
Relational Map has been included if there is any confusion.
CREATE TABLE PROFESSOR(
PSSN int NOT NULL,
PName varchar(255) NOT NULL,
PStreet varchar(255) NOT NULL,
PCity varchar(255) NOT NULL,
PState varchar(255) NOT NULL,
PZip int NOT NULL,
PArea int NOT NULL,
PNum int NOT NULL,
PSex ENUM ('M','F') NOT NULL,
PTitle varchar(255) NOT NULL,
PSalary int NOT NULL,
PRIMARY KEY (PSSN)
);
CREATE TABLE DEGREES(
ProfessorSSN int NOT NULL,
PDegrees varchar(255) NOT NULL,
PRIMARY KEY (ProfessorSSN, PDegrees),
FOREIGN KEY (ProfessorSSN) REFERENCES PROFESSOR (PSSN)
);
CREATE TABLE DEPARTMENT(
DNum int NOT NULL,
DName varchar(255) NOT NULL,
DPhone varchar(255) NOT NULL,
DOffice_Location varchar(255) NOT NULL,
PChairSSN int NOT NULL,
PRIMARY KEY (DNum),
FOREIGN KEY (PChairSSN) REFERENCES PROFESSOR (PSSN)
);
CREATE TABLE PRE_REQ(
PreReqCNum int NOT NULL,
PreReqFor varchar(255) NOT NULL,
PreReqTo varchar(255) NOT NULL,
FOREIGN KEY (PreReqCNum) REFERENCES COURSE (CNum) ON DELETE CASCADE
);
CREATE TABLE COURSE(
CNum int NOT NULL,
CTitle varchar(255) NOT NULL,
CUnits int NOT NULL,
CTextBook varchar(255) NOT NULL,
Department_Num int NOT NULL,
PRIMARY KEY (CNum),
FOREIGN KEY (Department_Num) REFERENCES DEPARTMENT (DNum)
);
CREATE TABLE MINOR_DEGREES(
MinorCWID int NOT NULL,
MinorDNum int NOT NULL,
Minor varchar(255) NOT NULL,
PRIMARY KEY (MinorCWID, MinorDNum),
FOREIGN KEY (MinorCWID) REFERENCES STUDENT (SCWID) ON DELETE CASCADE,
FOREIGN KEY (MinorDNum) REFERENCES DEPARTMENT (DNum) ON DELETE CASCADE
);
CREATE TABLE STUDENT(
SCWID int NOT NULL,
SFname varchar(255) NOT NULL,
SLname varchar(255) NOT NULL,
SAdrress varchar(255) NOT NULL,
SPhone int NOT NULL,
Major varchar(255) NOT NULL,
MajorDeptNum int NOT NULL,
PRIMARY KEY (SCWID),
FOREIGN KEY (Major, MajorDeptNum).
);
CREATE TABLE SECTION(
CourseNum int NOT NULL,
SNum int NOT NULL,
Classroom varchar(255) NOT NULL,
Meet_Dates varchar(255) NOT NULL,
Time_Start int NOT NULL,
Time_End int NOT NULL,
No_Of_Seats int NOT NULL,
ProSSN int NOT NULL,
PRIMARY KEY (CourseNum, SNum),
FOREIGN KEY (CourseNum) REFERENCES COURSE (CNum),
FOREIGN KEY (ProSSN) REFERENCES PROFESSOR (PSSN)
);
CREATE TABLE STUDENT_RECORD(
Student_SWID int NOT NULL,
Course_Number int NOT NULL,
Section_Number int NOT NULL,
Grade varchar(255)
PRIMARY KEY (Student_SWID, Course_Number, Section_Number),
FOREIGN KEY (Student_SWID) REFERENCES STUDENT (SCWID)
);
And the errors
Error for MINOR_DEGREES:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'REFERENCES STUDENT (SCWID) ON DELETE CASCADE,
UNIQUE KEY (MinorDNum) REFERENCE' at line 6
Error for STUDENT:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual. that corresponds to your MariaDB server version for the right syntax to use near ')' at line 11
Error for STUDENT_RECORD:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual. that corresponds to your MariaDB server version for the right syntax to use. near '(Student_SWID, Course_Number, Section_Number),
FOREIGN KEY (Student_SWID) RE' at line 6.

FOREIGN KEY constraint help Mysql

I'm reviewing mysql and I am working on composing foreign keys when creating tables. However when I create the foreign key I receive a syntax error.
CREATE TABLE IF NOT EXISTS staff (
staff_id INT(5) NOT NULL,
staff_first_name VARCHAR(20) NOT NULL,
staff_last_name VARCHAR(20) NOT NULL,
staff_phone_number VARCHAR(15) NOT NULL,
staff_email_address VARCHAR(30) NOT NULL,
CONSTRAINT staff_id_pk PRIMARY KEY (staff_id)
CONSTRAINT staff_id_fk FOREIGN KEY (staff_id)
REFERENCES computer_staff (staff_id)
);
Can anyone see what I've done wrong here?
EDIT:
The error I receive reports as:
19:35:55 CONSTRAINT staff_id_fk FOREIGN KEY (staff_id), REFERENCES computer_staff (staff_id)) Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT staff_id_fk FOREIGN KEY (staff_id), REFERENCES computer_staff (staff_' at line 1 0.00025 sec
UPDATE: Error Code: 1215. Cannot add foreign key constraint 0.110 sec
How does one resolve error code 1215??
You are missing comma:
CREATE TABLE IF NOT EXISTS staff (
staff_id INT(5) NOT NULL,
staff_first_name VARCHAR(20) NOT NULL,
staff_last_name VARCHAR(20) NOT NULL,
staff_phone_number VARCHAR(15) NOT NULL,
staff_email_address VARCHAR(30) NOT NULL,
CONSTRAINT staff_id_pk PRIMARY KEY (staff_id), -- here
CONSTRAINT staff_id_fk FOREIGN KEY (staff_id)
REFERENCES computer_staff (staff_id)
);
Try this code
CREATE TABLE IF NOT EXISTS staff (
staff_id INT(5) NOT NULL,
staff_first_name VARCHAR(20) NOT NULL,
staff_last_name VARCHAR(20) NOT NULL,
staff_phone_number VARCHAR(15) NOT NULL,
staff_email_address VARCHAR(30) NOT NULL,
Foregn_Key int,
CONSTRAINT staff_id_pk PRIMARY KEY (staff_id),
CONSTRAINT staff_id_fk FOREIGN KEY (Foregn_Key)
REFERENCES computer_staff (Foregn_Key)
);

Error code:1215 Cannot add foreign key constraint while running script in MySQL workbench version 6.3

First of all i apologize that the names of the tables and so on are in another language.
The issue is i can't seem to add a foreign key named ID_KOPIE from the table KOPIA_KNIHY into the table SKLAD for some reason. When it gets to adding the foreign key to to the table SKLAD it throws out an error 1215. Here is the code:
CREATE TABLE BOOK (
BOOK_NAME VARCHAR(30) NOT NULL,
YEAR CHAR(4) NOT NULL,
NAME_OF_EDITOR VARCHAR(30) NOT NULL,
WRITER_ID INTEGER NOT NULL,
ISBN VARCHAR(17) NOT NULL,
BOOK_ID INTEGER NOT NULL,
PRIMARY KEY (BOOK_ID),
);
CREATE TABLE BOOK_COPY(
BOOK_ID INTEGER NOT NULL,
LANGUAGE_CODE CHAR(3) NOT NULL,
COPY_ID INTEGER NOT NULL,
BOOK_PICTURES CHAR(1) NOT NULL
CHECK (BOOK_PICTURES IN ("Y", "N")),
PRIMARY KEY (BOOK_ID, LANGUAGE_CODE, COPY_ID)
FOREIGN KEY(BOOK_ID)
REFERENCES BOOK(BOOK_ID),
);
CREATE TABLE STORAGE (
BOOK_ID INTEGER NOT NULL,
COPY_ID INTEGER NOT NULL,
BUILDING_ID INTEGER NOT NULL,
ROOM_NUMBER NUMERIC(4,0) NOT NULL,
SHELF_NUMBER NUMERIC(4,0) NOT NULL,
PRIMARY KEY(BOOK_ID, BUILDING_ID, COPY_ID),
FOREIGN KEY(COPY_ID)
REFERENCES BOOK_COPY(BOOK_ID),
)
I researched the error code 1215 on the internet, i couldn't find anything wrong with my database. I checked if there's a typo or if i didn't forget to add the reference.
This is the error:
0 769 18:19:37 CREATE TABLE STORAGE (
BOOK_ID INTEGER NOT NULL,
COPY_ID INTEGER NOT NULL,
BUILDING_ID INTEGER NOT NULL,
ROOM_NUMBER NUMERIC(4,0) NOT NULL,
SHELF_NUMBER NUMERIC(4,0) NOT NULL,
PRIMARY KEY(BOOK_ID, BUILDING_ID, COPY_ID),
FOREIGN KEY(COPY_ID)
REFERENCES BOOK_COPY(BOOK_ID),
)
Error Code: 1215. Cannot add foreign key constraint 0.016 sec
My question is how can this be fixed that it would work.
Help would be greatly appreciated.
Try this way. Please do alter the ON UPDATE ... ON DELETE syntax in this example with the one you need.
CREATE TABLE `KNIHA` (
`NAZOV_KNIHY` VARCHAR(30) NOT NULL,
`ROK_PRVEHO_VYDANIA` CHAR(4) NOT NULL,
`NAZOV_VYDAVATELA` VARCHAR(30) NOT NULL,
`ID_AUTORA` INTEGER NOT NULL,
`ISBN` VARCHAR(17) NOT NULL,
`ID_KNIHY` INTEGER NOT NULL,
PRIMARY KEY (`ID_KNIHY`)
);
CREATE TABLE `KOPIA_KNIHY` (
`ID_KNIHY` INTEGER NOT NULL,
`KOD_JAZYKA` CHAR(3) NOT NULL,
`ID_KOPIE` INTEGER NOT NULL,
`ORAZKY_V_KNIHE` CHAR(1) NOT NULL
CHECK (`OBRAZKY_V_KNIHE` IN ("A", "N")),
INDEX(`ID_KOPIE`),
PRIMARY KEY (`ID_KNIHY`, `KOD_JAZYKA`, `ID_KOPIE`),
CONSTRAINT `idx_1` FOREIGN KEY `idx_1` (`ID_KNIHY`) REFERENCES `KNIHA`(`ID_KNIHY`) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE `SKLAD` (
`ID_KNIHY` INTEGER NOT NULL,
`ID_KOPIE` INTEGER NOT NULL,
`ID_BUDOVY` INTEGER NOT NULL,
`CISLO_MIESTNOSTI` NUMERIC(4,0) NOT NULL,
`CISLO_REGALU` NUMERIC(4,0) NOT NULL,
PRIMARY KEY(`ID_KNIHY`, `ID_BUDOVY`, `ID_KOPIE`),
CONSTRAINT `idx_2` FOREIGN KEY `idx_2` (`ID_KOPIE`) REFERENCES `KOPIA_KNIHY`(`ID_KOPIE`) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT `idx_3` FOREIGN KEY `idx_3` (`ID_KNIHY`) REFERENCES `KNIHA`(`ID_KNIHY`) ON UPDATE CASCADE ON DELETE CASCADE
);
Try it on SQL Fiddle
.

Error 1064 (42000 in mysql code

When i put my code in
CREATE TABLE EVENT (
Event_ID SMALLINT PRIMARY KEY NOT NULL AUTO_INCREMENT,
Event_Name VARCHAR(30) NOT NULL,
Event_Date DATE NOT NULL,
Event_Route VARCHAR(40) NOT NULL,
Event_Location VARCHAR(60) NULL,
Event_Cost DECIMAL(8,2) NOT NULL,
Staff_ID SMALLINT FOREIGN KEY NOT NULL,
Package_ID SMALLINT FOREIGN KEY NOT NULL,
CONSTRAINT Staff_ID FOREIGN KEY (Staff_ID) REFERENCES STAFF (Staff_ID),
CONSTRAINT Package_ID FOREIGN KEY (Package_ID) REFERENCES PACKAGE (Package_ID)
);
I get this error message below
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'FOREIGN KEY NOT NULL, Package_ID SMALLINT FOREIGN
KEY NOT NULL, CONSTRAINT Staff' at line 8
I think the error is pretty clear. You have key words in the middle of the definition of Package_Id and Staff_Id (FOREIGN KEY). These are not necessary, they are handled by the constraint definition:
CREATE TABLE EVENT (
Event_ID SMALLINT PRIMARY KEY NOT NULL AUTO_INCREMENT,
Event_Name VARCHAR(30) NOT NULL,
Event_Date DATE NOT NULL,
Event_Route VARCHAR(40) NOT NULL,
Event_Location VARCHAR(60) NULL,
Event_Cost DECIMAL(8,2) NOT NULL,
Staff_ID SMALLINT NOT NULL,
Package_ID SMALLINT NOT NULL,
CONSTRAINT Staff_ID FOREIGN KEY (Staff_ID) REFERENCES STAFF (Staff_ID),
CONSTRAINT Package_ID FOREIGN KEY (Package_ID) REFERENCES PACKAGE (Package_ID)
);
I would dissuade you from naming the table Event or even Events. The name is not reserved, but it is a keyword in MySQL.