Trying to make a foreign key in a table, SQL throws an error

T.C :

Within my table of studentTakingModule I am trying to set the column of pageID in there a foreign key, which leads back to the primary key in the table of pages. Please can someone guide me as to where I have gone wrong as SQL throws this error at me:

SQL Error

DB Design: DB

Lajos Arpad :

Run

select studentTakingModule.PageID
from studentTakingModule
where not exists (
    select 1
    from pages
    where pages.PageID = studentTakingModule.PageID
);

Presumably (based on the error message you received) the result will be non-empty. The values in the result are PageID values which do not exist in the pages table. If such values exist, you cannot create that foreign key. You will need to make sure that all the not null PageID values in your studentTakingModule exist as pages.PageID. Otherwise the reference your record should point to would not exist. This is what the error message is telling you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33255&siteId=1