0

I have a database 'STUDENT'. There are 4 table 'INFO','STREAM','GRADE' & 'MARKS'.

  • INFO has 3 columns ID, NAME, STR_NO.
  • STREAM has 2 columns STR_NO, STR_NAME.
  • GRADE has 2 columns PERCENT, GRADE.
  • MARKS has 3 columns ID, STR_NO, TOTAL_MARK, FULL_MARK

I have declared a composite primary key on info table i.e

PRIMARY KEY(ID, STR_NO)

But when I declare a foreign key on STREAM i.e

FOREIGN KEY(STR_NO) REFERENCES INFO(ID,STR_NO)

It gives error

Why and how to solve this

user5499810
  • 41
  • 1
  • 9

2 Answers2

0

If I understand what your tables are representing, it looks like you have the foreign key relationship backwards. It looks like STR_NO should be the primary key on STREAM, and that there should be a foreign key constraint on INFO requiring that INFO.STR_NO have a matching value in STREAM.STR_NO - or, in code

ALTER TABLE INFO
  ADD CONSTRAINT INFO_FK1
    FOREIGN KEY (STR_NO) REFERENCES STREAM(STR_NO);

Best of luck.

0

Do this:

ALTER TABLE STREAM ADD(ID NUMBER)

ALTER TABLE INFO
  ADD CONSTRAINT INFO_FK1
    FOREIGN KEY (ID, STR_NO) REFERENCES STREAM(ID, STR_NO);

Since Table Info has a composite primary key ID, STR_NO, then any reference to it must also include both columns.

For more detail, see here.

Community
  • 1
  • 1
Muhammad Muazzam
  • 2,810
  • 6
  • 33
  • 62