Answer: Cascading Delete
Explore how to implement cascading deletes in SQL to automatically remove dependent records. Understand altering tables, foreign key constraints, and the importance of ON DELETE CASCADE in managing referential integrity.
Solution
The solution is given below:
Code explanation
The explanation of the solution code is given below:
Line 2: This retrieves all the values from the
Skillstable.Lines 4–7: The
ALTER TABLEstatement alters theSkillstable to add aFOREIGN KEYconstraint namedFK_EmpID. It linksEmpIDinSkillstoEmpIDinEmployees, withON DELETE CASCADEto remove associated skills if an employee is deleted.Lines 9–10: This deletes the employee with
EmpID = 3from theEmployeestable.Line 12: This retrieves all the values from the
Skillstable again, reflecting the changes after the employee deletion.
Recalling relevant concepts
We have ...