Subject Re: Foreign key reference target does not exist. [FB 2.5 beta 2]
Author homerjones1941
> > What is the best way to delete orphans? ...

> The simple way:
>
> DELETE FROM MySecondTable MST
> WHERE NOT EXISTS(
> SELECT *
> FROM MyFirstTable MFT
> WHERE MFT.MyPrimaryKey = MST.MyFieldSoonToBeAForeignKey)
>
> I would consider selecting and inspecting the records before deleting them, you could e.g. issue
>
> SELECT DISTINCT MST.MyFieldSoonToBeAForeignKey
> FROM MySecondTable MST
> WHERE NOT EXISTS(
> SELECT *
> FROM MyFirstTable MFT
> WHERE MFT.MyPrimaryKey = MST.MyFieldSoonToBeAForeignKey)
>
> HTH,
> Set
>

Thank you Svein. I will probably incorporate both of these in a small utility program for my end users. I like your second option, which will allow the user to decide if they wish to delete, or re-assign the orphan to an existing master record. BTW, I really appreciate the sample SQL. Thanks again.