Answer: Cascading Delete
Find a detailed explanation about using referential integrity constraints.
Solution
The solution is given below:
Press + to interact
-- The query to enforce referential integrity constraintsSELECT * FROM Skills;ALTER TABLE SkillsADD CONSTRAINT FK_EmpIDFOREIGN KEY (EmpID) REFERENCES Employees(EmpID)ON DELETE CASCADE;DELETE FROM EmployeesWHERE EmpID = 3;SELECT * FROM Skills;
Code explanation
The explanation of the solution code is given below:
Line 2: This retrieves all the values from the
Skills
table.Lines 4–7: The
ALTER TABLE
statement alters theSkills
table to add aFOREIGN KEY
constraint namedFK_EmpID
. It linksEmpID
inSkills
toEmpID
inEmployees
, withON DELETE CASCADE
to remove associated skills if an employee is deleted.Lines 9–10: This deletes the employee with
EmpID = 3
from theEmployees
table.Line 12: This retrieves all the values from the
Skills
table again, reflecting the changes after the employee deletion.
Recalling relevant concepts
We have ...