FOREIGN KEY constraint help Mysql - 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)
);

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

Cannot add foreign key constraint for mysql

CREATE TABLE EMPLOYEE
(enum VARCHAR(5) NOT NULL,
first_name VARCHAR(30) NOT NULL,
Last_name VARCHAR(30) NOT NULL,
Salary decimal(8,2) NOT NULL,
CONSTRAINT EMPLOYEE_pkey PRIMARY KEY(enum));
CREATE TABLE LECTURER
(enum VARCHAR(5) NOT NULL,
lecturerNum CHAR(10) NOT NULL,
ranK VARCHAR(2) NOT NULL,
bioDate VARCHAR(200) NOT NULL,
CONSTRAINT pk_enum primary key(enum),
CONSTRAINT LECTURER_fkey FOREIGN KEY (enum) REFERENCES EMPLOYEE(enum),
CONSTRAINT lect_rank CHECK(rank in('L1'or'L2'or'L3')));
CREATE TABLE MANAGER
(enum VARCHAR(5) NOT NULL,
role VARCHAR(10) NOT NULL,
CONSTRAINT pk_enum primary key(enum),
CONSTRAINT MANAGER_fkey FOREIGN KEY(enum)
REFERENCES EMPLOYEE(enum) ON DELETE CASCADE);
CREATE TABLE SUBJECT
(subjectCode CHAR(10) NOT NULL,
SubjectName CHAR(30) NOT NULL,
credit decimal(3,3) NOT NULL,
taughtBy char(10) NOT NULL,
CONSTRAINT SUBJECT_PK PRIMARY KEY(subjectCode),
CONSTRAINT SUBJECT_fkey FOREIGN KEY(taughtBy)
REFERENCES LECTURER(lecturerNum) ON DELETE CASCADE,
CONSTRAINT CREDIT_CHECK CHECK(credit >0));
I did not find any error on my create table, why when I run it at the subject table it says:
ERROR 1215 (HY000): Cannot add foreign key constraint
Why..... can anyone help me with it?
LECTURER.lecturerNum does not have an index, which is required to be able to use it in a FK constraint:
CREATE TABLE LECTURER
(enum VARCHAR(5) NOT NULL,
lecturerNum CHAR(10) NOT NULL,
ranK VARCHAR(2) NOT NULL,
bioDate VARCHAR(200) NOT NULL,
KEY lecturerNum (lecturerNum),
CONSTRAINT pk_enum primary key(enum),
CONSTRAINT LECTURER_fkey FOREIGN KEY (enum) REFERENCES EMPLOYEE(enum),
CONSTRAINT lect_rank CHECK(rank in('L1'or'L2'or'L3')));

MySQL foreign key constraint error code 1215

Can anyone see the problem with my create statement for table contactgroup. I keep getting error code 1215 can't create foreign key constraint.
create table contact(
ContactID int(5) not null auto_increment,
ContactName varchar(255) Not null,
ContactNumber int(5),
ContactEmail varchar(255),
primary key(ContactID))ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table contactgroup(
ContactGroupID int(5) Not Null,
ContactID int(5) Not Null,
primary key(ContactGroupID),
key fk_contactgroup_ContactID (ContactID),
constraint fk_contactgroup_ContactID
foreign key(ContactID)
references contact) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You need to mention both the table and the primary key column of that table when defining a foreign key constraint:
FOREIGN KEY (ContactID) REFERENCES contact(ContactID)

I get an error when I create a table including primary key and foreign key

I tried to create a table with primary and foreign key but it gives me an error saying
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 '),
projectLeader INT(3),
PRIMARY KEY (empNo, projNo),
FOREIGN KEY (empNo) REFERE' at line 4
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 '),
totalCharge DOUBLE(8),
PRIMARY KEY (empNo, jobClass),
FOREIGN KEY (empNo) REF' at line 4
the table codes that gets error messages are:
CREATE TABLE projectInfo(
empNo INT(3) not null,
projNo INT(2) not null,
hourBill DOUBLE(4),
projectLeader INT(3),
PRIMARY KEY (empNo, projNo),
FOREIGN KEY (empNo) REFERENCES employee (empNo),
FOREIGN KEY (projNo) REFERENCES project (projNo)
);
CREATE TABLE workInfo(
empNo INT(3) not null,
jobClass VARCHAR(4) not null,
hourCharge DOUBLE(8),
totalCharge DOUBLE(8),
PRIMARY KEY (empNo, jobClass),
FOREIGN KEY (empNo) REFERENCES employee (empNo),
FOREIGN KEY (jobClass) REFERENCES job (jobClass)
);
and this is the whole table command.
DROP database IF EXISTS projects;
CREATE database projects;
USE projects;
DROP TABLE IF EXISTS employee;
CREATE TABLE employee(
empNo INT(3) not null,
empName VARCHAR(25) not null,
PRIMARY KEY (empNo)
);
CREATE TABLE project(
projNo INT(2) not null,
projName VARCHAR(25) not null,
PRIMARY KEY (projNo)
);
CREATE TABLE job(
jobClass VARCHAR(4) not null,
jobDesc VARCHAR(25) not null,
PRIMARY KEY (jobClass)
);
CREATE TABLE projectInfo(
empNo INT(3) not null,
projNo INT(2) not null,
hourBill DOUBLE(4),
projectLeader INT(3),
PRIMARY KEY (empNo, projNo),
FOREIGN KEY (empNo) REFERENCES employee (empNo),
FOREIGN KEY (projNo) REFERENCES project (projNo)
);
CREATE TABLE workInfo(
empNo INT(3) not null,
jobClass VARCHAR(4) not null,
hourCharge DOUBLE(8),
totalCharge DOUBLE(8),
PRIMARY KEY (empNo, jobClass),
FOREIGN KEY (empNo) REFERENCES employee (empNo),
FOREIGN KEY (jobClass) REFERENCES job (jobClass)
);
if anyone has any idea plz help me.
thanks! :)

What is wrong with my foreign key in 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
);