Pergunta

One of the classic questions...

But I really can't find where the issue is in the parent table. I am currently working on a mini-version of the IMDB database. Our teacher tasked us with taking the original database we were given and make it into a smaller, and smarter version. It should follow this diagram:

Database Diagram

I have so far successfully made the COMPANY_NAME, MOVIE_COMPANY, MOVIE, GENRE and PLOT tables and is currently making KEYWORD. The table itself now exists, but I cannot place the Foreign constraint which indicates that the MOVIE_ID present in keyword is from the parent table MOVIE. I keep getting the following error:

ALTER TABLE KEYWORD
  ADD CONSTRAINT FK_MOVIE_ID_KEYWORD
    FOREIGN KEY (MOVIE_ID)
    REFERENCES MOVIE (ID)
Error report -
SQL Error: ORA-02298: cannot validate (DB_031.FK_MOVIE_ID_KEYWORD) - parent keys not found
02298. 00000 - "cannot validate (%s.%s) - parent keys not found"
*Cause:    an alter table validating constraint failed because the table has
           child records.
*Action:   Obvious <-- especially this part is annoying

I have looked around at similar questions and then checked my statement a few times, but I have so far been unable to see why this fails. The GENRE and PLOT tables are both empty in terms of data, as they are made for future expansion of the database. But KEYWORD already have a collection of KEYWORD, MOVIE_ID pairs.

EDIT: Request Schemas

Movie Schema

Keyword Schema

Foi útil?

Solução

You are referencing to wrong col for parent reference....it should be MOVIE_ID rather than ID ( which u have done )

try it this way :

ALTER TABLE KEYWORD
  ADD CONSTRAINT FK_MOVIE_ID_KEYWORD
    FOREIGN KEY (MOVIE_ID)
    REFERENCES MOVIE (MOVIE_ID) /* <- notice this */

EDIT

Only possible reason i can think of is that you table contains non-matching data....that's why you are having an error due to mismatch!!

To simplyfy....your MOVIE_ID contains value which do not exist in the ID column of the other table....so because of mismatch at the time of setting up constraint,its popping the error!!

Solution : Validate the data in your tables ( updating / deleting non-matching columns in either tables ) in relation to the id's you need to set up...then apply the constraint and it should work!!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top