SQLTeam.com | Weblogs | Forums

Delete records from multiple tables

Hi All.
The DataGrid is populated by join of two tables Department and EmployeeDept . I created DataGrid row button to delete a record. But I cannot delete the entry as it is related to other tables.

This diagram displays related tables. For example, if in the EmployeeDept table need to be delete record where DepartmentId=7 so in the EmployeeTask table should be deleted all records with related EmployeeDeptId number. If that is possible. How create SELECT to delete record. I will appreciate for detail explanation and sample.



If this is SQL Server, and you have created foreign keys (as implied in the diagram), then yes, you can do that.

You must create the foreign keys with "ON DELETE CASCADE". This "tells" SQL that when you delete for the main table, SQL should automatically delete from related tables ("cascade" the delete to other table(s)).