需要帮助SQL错误(ORA-02270):此列列表错误没有匹配的唯一键或主键

需要帮助SQL错误(ORA-02270):此列列表错误没有匹配的唯一键或主键,sql,oracle,Sql,Oracle,我有多张桌子 -学生(学生ID[pk],学生姓名) -合格(FID[pk],课程ID[pk],日期Q) -教员(FID[pk],Fname) -课程(课程ID[pk],课程名称) 我还需要再创建两个,分别是分区和注册 -章节(章节编号[pk],学期[pk],课程ID[pk]) -注册(学号[pk],章节号[pk],学期[pk]) 我首先创建没有任何问题的部分: create table section( SectionNo number(28) not null, Semester varcha

我有多张桌子

-学生(学生ID[pk],学生姓名)

-合格(FID[pk],课程ID[pk],日期Q)

-教员(FID[pk],Fname)

-课程(课程ID[pk],课程名称)

我还需要再创建两个,分别是分区和注册

-章节(章节编号[pk],学期[pk],课程ID[pk])

-注册(学号[pk],章节号[pk],学期[pk])

我首先创建没有任何问题的部分:

create table section(
SectionNo number(28) not null,
Semester varchar(25) not null,
CourseID varchar(25) not null,
constraint sec_pk primary key(SectionNo,Semester,CourseID),
constraint sec_fk foreign key(CourseID) references Course(CourseID)
on delete cascade);
然后我尝试创建一个名为registration的表,但它在标题中给出了错误

create table registration(
StudentID number(28) not null,
SectionNo number(28) not null,
Semester varchar(25) not null,
constraint reg_pk primary key(SectionNo,StudentID,Semester),
constraint reg_fk foreign key(StudentID) references Student(StudentID)
on delete cascade,
constraint reg_fk2 foreign key(SectionNo,Semester) references 
Section(SectionNo,Semester) on delete cascade);

有人能帮我找出问题所在吗

ORA-2270错误非常简单:当我们在外键中引用的列与主键或父表上的唯一约束不匹配时,就会发生错误

这里,在您的例子中,section表的主键是(SectionNo,sement,CourseID),而您只引用section(SectionNo,sement)

要消除此问题,请在您的辅助密钥中添加“CourseID”


好的阅读:

ORA-2270错误非常简单:当我们在外键中引用的列与父表上的主键或唯一约束不匹配时,就会发生错误

这里,在您的例子中,section表的主键是(SectionNo,sement,CourseID),而您只引用section(SectionNo,sement)

要消除此问题,请在您的辅助密钥中添加“CourseID”


一本好书:

问题是在分区表中找不到“CourseID”,因此我无法将其添加到我的外键语句中。我不确定您在这里的意思。我可以清楚地看到列:“CourseID varchar(25)not null”以及列上的约束定义主键:“constraint secu_pk primary key(SectionNo,sement,CourseID)”这是您希望我添加到代码中的内容吗?删除级联上的constraint reg_fk2外键(SectionNo,Serm)引用节(SectionNo,Serm,CourseID);请在创建第二个表时添加以下内容:constraint reg_fk2外键(SectionNo,Sembert,CourseID)在删除级联上引用SectionNo,Sembert,CourseID)它没有,我必须向registration中添加一个名为courseid的额外列,该列只保存空值,以便像您上面所做的那样生成外键约束。问题是在section表中找不到“courseid”,因此我无法将其添加到我的外键语句中。我不确定您在这里的意思是什么。我可以清楚地看到列:“CourseID varchar(25)not null”以及列上的约束定义主键:“constraint secu_pk primary key(SectionNo,sement,CourseID)”这是您希望我添加到代码中的内容吗?删除级联上的constraint reg_fk2外键(SectionNo,Serm)引用节(SectionNo,Serm,CourseID);请在创建第二个表时添加以下内容:constraint reg_fk2外键(SectionNo,Sembert,CourseID)在删除级联上引用SectionNo,Sembert,CourseID)它没有,我必须在注册中添加一个名为courseid的额外列,该列只保存空值,以便像上面一样生成外键约束。