Delete unmatching records in the child table

  • Hello All,

    I've 2 tables ResumeSkill (Child table) and Skill (Parent table), There are duplicates in the parent table and after removing the foreign key constraint in child table deleted all duplicate values from Parent table. But those deleted duplicate values has references in child table which need to be deleted now. Can anyone please let me know the query?

    ResumeSkill Skill

    Id SkillId

    SkillId Name

    I want to delete all the records from ResumeSkill that dont have matching skillId in Skill table.

    Thanks,

  • maruthipuligandla (2/24/2015)


    Hello All,

    ResumeSkill Skill

    Id SkillId

    SkillId Name

    I want to delete all the records from ResumeSkill that don't have matching skillId in Skill table.

    Thanks,

    DELETE RS

    FROM ResumeSkill RS

    WHERE NOT EXISTS

    (SELECT 1 FROM SkillTable ST

    WHERE RS.Skill = ST.SkillID)

    Without DDL and data, this code is completely untested. First, convert the above statement to a Select to make sure it returns the rows you really want to delete. After you are satisfied that the Select is working, change it back to a delete and it should delete exactly the same number of rows as you saw in the Select.

    Test, test, test!

    __________________________________________________________________________________________________________
    How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • The select query worked, i've deleted the unmatching records earlier and when i ran this query it didnt return any rows..

    Thanks alot!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply