0

This is what I have so far:

CREATE TABLE GRADE
(    
    COURSE_NO   VARCHAR2(8),
    STUDENT_ID  VARCHAR2(15), 
    GRADE   VARCHAR2(3), 
    PRIMARY KEY(COURSE_NO,STUDENT_ID) ENABLE
);

ALTER TABLE GRADE 
ADD FOREIGN KEY (COURSE_NO)
REFERENCES COURSE (COURSE_NO)ENABLE;

ALTER TABLE GRADE 
ADD FOREIGN KEY (STUDENT_ID)
REFERENCES STUDENT (STUDENT_ID) 
ON DELETE CASCADE ENABLE;

INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE) 
    VALUES ('CS100','123','B');  
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('CS100','234','A');   
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('CS100','456','B');   
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('CS100','567','A');   
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('CS200','678','B');   
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('CS200','789','C');
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('CS200','890','A');
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('CS200','901','A');
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('EE100','012','B');
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('EE100','013','A');  
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('EE100','014','C');   
INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('EE100','015','B');  


INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('EE220','016','B');           -- This one doesn't work

INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
       VALUES('EE200','017','A');            

INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('EE200','018','B');           -- this one either


INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
       VALUES('MA220','019','B');

INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
     VALUES('MA220','020','B');                     


INSERT INTO GRADE(COURSE_NO,STUDENT_ID,GRADE)   
    VALUES('MA220','021','B');           -- This insert doesn't work

closed as off-topic by Balazs Papp, Philᵀᴹ, Colin 't Hart, Michael Green, ypercubeᵀᴹ Oct 5 '15 at 10:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Too localized - this could be because your code has a typo, basic error, or is not relevant to most of our audience. Consider revising your question so that it appeals to a broader audience. As it stands, the question is unlikely to help other users (regarding typo questions, see this meta question for background)." – Balazs Papp, Colin 't Hart, Michael Green, ypercubeᵀᴹ
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What means: "This does not work"? Is there an error message? If so, which error message? – miracle173 Oct 5 '15 at 9:25
  • I've voted to close as foreign key constraints are a foundational concept. – Michael Green Oct 5 '15 at 10:49
2

The foreign keys are defined on course_no and student_id. Assuming the error is a foreign key violation (big assumption, please post the full error in future questions) it must be one of those two at fault. Other rows reference course EE200 so it's not that; so student 018 doesn't exist in the student table. Similarly MA220 so student 021 is missing. With EE220 / 016 there are no comparators so either or both could be missing. Given your record I'd suggest the student.

This is basic stuff and fundamental to the concept of foreign keys. You should read up on the concept and ensure you write data in a sequence that respects the constraints you have defined. Otherwise you are in for a world of pain.

1

You need to look at the data in STUDENT and COURSE. It seems that you are missing data in those tables, which causes your GRADE insert to fail in order to maintain data integrity.

Not the answer you're looking for? Browse other questions tagged or ask your own question.