/*
delete_phantom_citation_link_7.sql  (RM7 version)
Jerry Bryan    6/18/2024

Deletes citation links in CitationTable which
are phantom.
*/

DELETE FROM CitationTable
WHERE CitationID IN
(
SELECT C.CitationID
FROM CitationTable AS C 
LEFT JOIN PersonTable AS P ON P.PersonID = C.OwnerID
WHERE C.OwnerType = 0 AND P.PersonID IS NULL

UNION

SELECT C.CitationID
FROM CitationTable AS C 
LEFT JOIN FamilyTable AS F ON F.FamilyID = C.OwnerID 
WHERE C.OwnerType = 1 AND F.FamilyID IS NULL

UNION

SELECT C.CitationID
FROM CitationTable AS C
LEFT JOIN EventTable AS E ON E.EventID = C.OwnerID
WHERE C.OwnerType = 2 AND E.EventID IS NULL

UNION

SELECT C.CitationID
FROM CitationTable AS C
LEFT JOIN NameTable AS N ON N.NameID = C.OwnerID
WHERE C.OwnerType = 7 AND N.NameID IS NULL
)