Hi I have 开发者_开发百科2 tables Document and Project.
DocumentID is an FK in the Project table.
Using sql How can I delete Document Records in the Document table, and also remove their corresponding records in the Project table.
Thanks
When creating the foreign key, specify is as a ON DELETE CASCADE table constraint.
This constraint means that when a document is deleted, all project rows referencing it as a foreign key will also be deleted.
delete
from projects
where documentsFK = (
select documentFK
from documents
where documentsFK > 125
);
delete
from documents
where documentsFK > 125;
EDIT
delete
from projects
where documentsFK in (
select documentFK
from documents
where documentsFK > 125
);
delete
from documents
where documentsFK > 125;
精彩评论