1

I'm having a problem with a FOREIGN KEY. I'm always getting

FOREIGN KEY (STUDID)
            *

ERROR at line 3:
ORA-00904: "STUDID": invalid identifier
1
  • The behavior you are observing is expected. In order to add a foreign key constraint, the column(s) being referenced in the constraint must exist. If you add the column, e.g. ALTER TABLE SUBJECT ADD STUDID NUMBER(10) COMMENT 'fk ref student.studid', then the statement that adds the foreign key constraint will succeed. That fixes the error, but see the answer from Ditto: this looks way more like a many-to-many relationship, i.e. a subject should be allowed to be related to more than one student. But if you want a student related to at most one subject, then add SUBJECTID column to STUDENT Commented Mar 30, 2015 at 19:14

2 Answers 2

1

You have no field called STUDID in your SUBJECT table. Looks like you're trying to create a foreign key on a field that doesn't exist.

Sign up to request clarification or add additional context in comments.

Comments

1

Carson is correct - it seems like you are trying to build a relationship between Students and Subjects. Typically this is a many to many relationship, and would likely require a third, middle table to link them.

Think

"A Student can be enrolled to many subjects."

"A Subject can have many Students enrolled."

So you need a middle table with both STUDID and SUBJECTID columns, and that table would have 2 FKs declared, pointing to the other tables.

[edit] of course, that's the whole 3rd normal form thing .. you may or may not want/need to denormalize at some point, but that's a different discussion ;) [/edit]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.