问题描述
我在尝试创建表时收到此错误,但我不知道为什么:[2016-07-05 14:08:02] [42000][2270] ORA-02270: no matching unique or primary key for this column-list
I'm receiving this error when trying to create a table and I don't know why: [2016-07-05 14:08:02] [42000][2270] ORA-02270: no matching unique or primary key for this column-list
(对我来说)这个问题与,因为在该问题中,OP引用的表包含复合PK,而我不是.
This question seems different (to me) from a similar question, because in that question the OP is referencing a table with a composite PK, while I am not.
其他问题具有相同的错误代码,这是因为OP错误地引用了主键,而我却没有认为.
And while this other question has the same error code, it is because the OP is incorrectly references the primary key, which I don't think I did.
对SQL有更多经验的人可以教育我吗?
May someone more experienced in SQL educate me?
(需要注意的几件事:1)我知道表/列的名称与约定有一些错误/偏差,但这是为了家庭作业,老师要求我将表和行声明为确切地他的方式,即使它是非常规的. 2)是的,这很愚蠢;但不,我无法更改它或将其标记下来.)
(A couple of things to note: 1) I know the table/column names have small errors/deviations from convention, but this is for homework, and the teacher requires I have the tables and rows declared exactly his way, even if it's non-conventional. 2) Yes, that's silly; but no, I can't change it or I get marked down.)
CREATE TABLE Student_Course
(
Stu_ID NUMBER(5) NOT NULL,
Course_ID VARCHAR2(8) NOT NULL,
Section# NUMBER(3),
CONSTRAINT pk_stu_crse PRIMARY KEY (Stu_ID, Course_ID),
CONSTRAINT fk_course_id FOREIGN KEY (Course_ID) REFERENCES course(Course_ID),
CONSTRAINT fk_stu_id FOREIGN KEY (Stu_ID) REFERENCES student(Stu_ID),
CONSTRAINT fk_section FOREIGN KEY (Section#) REFERENCES course(Section#)
)
只有两个小的引用表,它们是:
There are only two, small, referenced tables, which are:
CREATE TABLE student
(
Stu_ID NUMBER(5) PRIMARY KEY ,
Lname VARCHAR2(20),
Fname VARCHAR2(20),
Mi CHAR(1),
Sex CHAR(1),
Major VARCHAR2(15),
Home_State CHAR(2)
);
CREATE TABLE course
(
Course_ID VARCHAR2(8) PRIMARY KEY ,
Section# NUMBER(3),
C_Name VARCHAR2(30),
C_Description VARCHAR2(30)
);
推荐答案
外键是对另一个表中主键的引用.
A foreign key is a reference to a primary key in another table.
最后一个约束CONSTRAINT fk_section FOREIGN KEY (Section#) REFERENCES course(Section#)
将不起作用-Section#
不是该表中的主键
The last constraint CONSTRAINT fk_section FOREIGN KEY (Section#) REFERENCES course(Section#)
won't work - Section#
isn't a primary key in that table
这篇关于Oracle SQL:收到“没有匹配的唯一键或主键"错误,并且不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!