错误代码:1826。重复的外键约束名称 'menu_ibfk_1'
Error Code: 1826. Duplicate foreign key constraint name 'menu_ibfk_1'
我正在 MySQL 上编写代码,但出现错误。
这是我的代码,它给我错误 - “错误代码:1826。重复的外键约束名称 'menu_ibfk_1'”
当我给一个不同的约束名称时,它给我错误 - “错误代码:1822。无法添加外键约束。在引用的 table 中缺少约束 'menu_ibfk_2' 的索引'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));
这是 MySQL 输出的快照
MySQL output snapshot
我做错了什么?
外键必须引用完整的键:
FOREIGN KEY (Datee, Timee, Dish) References Menu(Datee, Timee, Dish)
并且每个约束都必须有自己的唯一名称。
我正在 MySQL 上编写代码,但出现错误。
这是我的代码,它给我错误 - “错误代码:1826。重复的外键约束名称 'menu_ibfk_1'”
当我给一个不同的约束名称时,它给我错误 - “错误代码:1822。无法添加外键约束。在引用的 table 中缺少约束 'menu_ibfk_2' 的索引'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));
这是 MySQL 输出的快照
MySQL output snapshot
我做错了什么?
外键必须引用完整的键:
FOREIGN KEY (Datee, Timee, Dish) References Menu(Datee, Timee, Dish)
并且每个约束都必须有自己的唯一名称。