NonCreature0714
NonCreature0714

Reputation: 6014

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

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

Upvotes: 2

Views: 104

Answers (3)

NonCreature0714
NonCreature0714

Reputation: 6014

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

Upvotes: 1

Luc M
Luc M

Reputation: 17314

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

Upvotes: 2

Peter M.
Peter M.

Reputation: 713

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

Upvotes: 4

Related Questions