Search This Blog

Sunday, June 16, 2013

How to delete referential records from a sql table?

If there is a foreign key relationship between two table then you can not delete a record which is referenced by another table.

Solution:
Use On DELETE CASCADE

Example:
Alter Table Employee
DROP Constraint FK_Employee_Department
GO
ALTER Table Employee
Add Constraint FK_Employee_Department Foreign key (DeptID)
References Department(id)
On DElete Cascade


Note: If you delete a record from department table then all the associated records from Employee will be deleted.

No comments:

Post a Comment