SQL Server Cascading Referential integrity

niwlg2el  于 2023-03-17  发布在  其他
关注(0)|答案(1)|浏览(127)

I'm using Employee , City , EmpSalary , EmpOrganization , Salcomponents tables in my database where Employee table CityID column references CityID of City table, and reporting ID column self references EmpID of Employee table

  • Empsalary table empId references Employee.EmpId
  • EmpOrganization table empid references Employee.EmpId

I want to design a stored procedure with CityId as input to delete data from Employee , Empsalary , Emporganization simultaneously

Please suggest how to proceed further?

lkaoscv7

lkaoscv71#

You don't need a stored proceudre. You can design your foregin key constraints that they cascade the delete:

ALTER TABLE Employee
ADD FOREIGN KEY (CityId) REFERENCES City (Id) ON DELETE CASCADE

This will delete the entry in the City table when the Employee entry is deleted.

相关问题