Oracle SQL: Receiving 'no matching unique or primary key' error and don't know why

别来无恙 提交于 2019-12-14 03:54:46

问题


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

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.

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.

May someone more experienced in SQL educate me?

(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)
);

回答1:


A foreign key is a reference to a primary key in another table.

The last constraint CONSTRAINT fk_section FOREIGN KEY (Section#) REFERENCES course(Section#) won't work - Section# isn't a primary key in that table




回答2:


Section# Must be at least UNIQUE in course table.

If you want to use Section# as a reference for a foreign key, it must be a UNIQUE or a PRIMARY KEY

More information about FOREIGN KEY and constraints




回答3:


Thanks to good answers, I'm posting my code which I corrected based on help here. Hope my corrections help others in the future.

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) ,
  Section# NUMBER(3) ,
  C_Name VARCHAR2(30),
  C_Description VARCHAR2(30),
  CONSTRAINT pk_course PRIMARY KEY (Course_ID, Section#)
);

CREATE TABLE Student_Course
(
  Stu_ID NUMBER(5)  ,
  Course_ID VARCHAR2(8)  ,
  Section# NUMBER(3) ,
  CONSTRAINT pk_stu_crse PRIMARY KEY (Stu_ID, Course_ID, Section#),
  CONSTRAINT fk_stu FOREIGN KEY (Stu_ID) REFERENCES student(Stu_ID),
  CONSTRAINT fk_course_id FOREIGN KEY (Course_ID, Section#) REFERENCES course(Course_ID, Section#)
);


来源:https://stackoverflow.com/questions/38211271/oracle-sql-receiving-no-matching-unique-or-primary-key-error-and-dont-know-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!