-- TODO 11662 clientdelnotelinescan has no link to clientdeliverynoteline

delete from CLIENTDELNOTELINESCAN cds
 where cds.inscanclientdelnoteline_id is not null and not exists
 (select 1
          from clientdeliverynoteline cdl
         where cdl.clientdeliverynoteline_id =
               cds.inscanclientdelnoteline_id);
               
commit;               

delete from CLIENTDELNOTELINESCAN cds
 where cds.outscanclientdelnoteline_id is not null and not exists
 (select 1
          from clientdeliverynoteline cdl
         where cdl.clientdeliverynoteline_id =
               cds.outscanclientdelnoteline_id);
               
commit;               
               
ALTER TABLE CLIENTDELNOTELINESCAN ADD CONSTRAINT CLIENTDELNOTELINESCAN_ICDNL 
         FOREIGN KEY(INSCANCLIENTDELNOTELINE_ID) 
         REFERENCES CLIENTDELIVERYNOTELINE(CLIENTDELIVERYNOTELINE_ID) ON DELETE CASCADE;

ALTER TABLE CLIENTDELNOTELINESCAN ADD CONSTRAINT CLIENTDELNOTELINESCAN_OCDNL 
         FOREIGN KEY(OUTSCANCLIENTDELNOTELINE_ID) 
         REFERENCES CLIENTDELIVERYNOTELINE(CLIENTDELIVERYNOTELINE_ID) ON DELETE CASCADE;