我很難理解為什么我的代碼給了我無法添加外鍵約束錯誤。我剛開始在 MySQL 中編碼,所以我為任何愚蠢的事情感到抱歉。
drop table if exists COURSE_INSTRUCTORJG;
drop table if exists CLASSJG;
drop table if exists COURSEJG;
drop table if exists INSTRUCTORJG;
create table INSTRUCTORJG(
InstructorID int,
DepartHead int,
primary key(InstructorID)
);
create table COURSEJG(
CourseNo char(10),
ComputerNeeded smallint,
primary key(CourseNo)
);
create table CLASSJG(
Building CHAR(20),
RoomNum smallint,
Maxcap int,
primary key(Building, RoomNum)
);
create table COURSE_INSTRUCTORJG (
COURSEJG_CourseNo CHAR(10),
INSTRUCTORJG_InstructorID INT,
Semester CHAR(20),
Section INT,
CLASSJG_Building char(20),
CLASSJG_RoomNum smallint,
primary key(COURSEJG_CourseNo, INSTRUCTORJG_InstructorID, Semester, Section),
foreign key (COURSEJG_CourseNo) references COURSEJG(CourseNo),
foreign key (INSTRUCTORJG_InstructorID) references INSTRUCTORJG(InstructorID),
foreign key (CLASSJG_Building) references CLASSJG(Building),
foreign key (CLASSJG_RoomNum) references CLASSJG(RoomNum)
);
我的錯誤來自以下行:外鍵 (CLASSJG_RoomNum) 參考 CLASSJG(RoomNum)。
uj5u.com熱心網友回復:
您可以參考主鍵、鍵或唯一的 comnstrqaint,但在表 CLASSJG 中您有一個雙主鍵,因此請參考這兩個列或定義另一個鍵
create table CLASSJG(
Building CHAR(20),
RoomNum smallint,
Maxcap int,
primary key(Building, RoomNum),
KEY(RoomNum)
);
見樣品小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/438133.html
上一篇:根據資料框中的列選擇sql中的列
下一篇:如何將散列密碼存盤到資料庫?
