Skip to content
Advertisement

Error Code: 1826. Duplicate foreign key constraint name ‘menu_ibfk_1’

I am writing a code on MySQL and it’s giving me errors.

This is my code and it’s giving me the error – “Error Code: 1826. Duplicate foreign key constraint name ‘menu_ibfk_1’ “

When I give a different constraint name, it give me the error – “Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint ‘menu_ibfk_2’ in the referenced table ‘menu'”

create database WasteManagement;
CREATE TABLE WasteManagement.Faculty_login ( 
Faculty_ID int(9) NOT NULL  ,
FName varchar(50) DEFAULT NULL,
Department varchar(20) DEFAULT NULL, 
Password varchar(20) DEFAULT NULL, 
PRIMARY KEY (Faculty_ID) );

CREATE TABLE WasteManagement.Student_login ( 
Enrollment_No int(11) NOT NULL , 
SName varchar(50) DEFAULT NULL, 
Course varchar(20) DEFAULT NULL, 
Password varchar(20) DEFAULT NULL, 
PRIMARY KEY (Enrollment_No) );

CREATE TABLE WasteManagement.Staff_login ( 
Staff_ID int(9) NOT NULL  ,
CName varchar(50) DEFAULT NULL,
Username varchar(20) DEFAULT NULL, 
Password varchar(20) DEFAULT NULL, 
PRIMARY KEY (Staff_ID) );

CREATE TABLE WasteManagement.Menu ( 
Staff_ID int(9) NOT NULL,
Datee date NOT NULL DEFAULT 0,
Timee varchar(10) NOT NULL,
Dish varchar(30) NOT NULL, 
PRIMARY KEY (Datee, Timee, Dish)
#FOREIGN KEY (Staff_ID) References Menu(Staff_ID) 
);

CREATE TABLE WasteManagement.Demand1 ( 
Datee date NOT NULL DEFAULT 0,
Timee varchar(10) NOT NULL,
Dish varchar(30) NOT NULL, 
S_demand BOOLEAN DEFAULT FALSE, 
PRIMARY KEY (Datee, Timee, Dish, S_demand),
KEY d1 (datee),
KEY t1 (Timee),
KEY dish1 (Dish),
#KEY `AuthorID` (`AuthorID`),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Datee) References Menu(Datee),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Timee) References Menu(Timee),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Dish) References Menu(Dish));

CREATE TABLE WasteManagement.Demand2 ( 
Datee date NOT NULL DEFAULT 0,
Timee varchar(10) NOT NULL,
Dish varchar(30) NOT NULL, 
F_demand BOOLEAN NOT NULL DEFAULT FALSE, 
PRIMARY KEY (Datee, Timee, Dish, F_demand),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Datee) References Menu(Datee),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Timee) References Menu(Timee),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Dish) References Menu(Dish));

CREATE TABLE WasteManagement.Feedback1 ( 
Datee date NOT NULL DEFAULT 0,
Timee varchar(10) NOT NULL,
Dish varchar(30) NOT NULL, 
eaten_by1 int(7) NOT NULL, 
s_rating int(1) NOT NULL,
PRIMARY KEY (Datee, Timee, Dish, eaten_by1, s_rating),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Datee) References Menu(Datee),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Timee) References Menu(Timee),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Dish) References Menu(Dish));

CREATE TABLE WasteManagement.Feedback2 ( 
Datee date NOT NULL DEFAULT 0,
Timee varchar(10) NOT NULL,
Dish varchar(30) NOT NULL, 
eaten_by2 int(7) NOT NULL, 
f_rating int(1) NOT NULL,
PRIMARY KEY (Datee, Timee, Dish, eaten_by2, f_rating),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Datee) References Menu(Datee),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Timee) References Menu(Timee),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Dish) References Menu (Dish));

CREATE TABLE WasteManagement.Wastage ( 
Datee date NOT NULL DEFAULT 0,
Timee varchar(10) NOT NULL,
Dish varchar(30) NOT NULL, 
Produced_qty int(7) NOT NULL, 
Wasted_qty int(7) NOT NULL,
PRIMARY KEY (Datee, Time, Dish, Produced_qty, Wasted_qty),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Datee) References Menu(Datee),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Timee) References Menu(Timee),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (Dish) References Menu(Dish));

This is the snapshot of MySQL output

MySQL output snapshot

What am I doing wrong?

Advertisement

Answer

A foreign key must reference the complete key:

FOREIGN KEY (Datee, Timee, Dish) References Menu(Datee, Timee, Dish)

And each constraint must have its own, unique name.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement