Click here to Skip to main content
15,898,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a employee table and employee related tables. If i remove one employee from my table all data related to that employee should be removed from all other tables
Posted
Comments
ZurdoDev 14-Aug-12 8:53am    
Do you have foreign keys setup with cascading deletes? Do you have triggers? There are several ways to do this.
Vedangi 14-Aug-12 8:59am    
You can write trigger on Employee table to delete all related data from Employee releated table.

What is your question?Do you need sql query for your operation or you cannot do that?You can try with it..
DELETE user,items,orders FROM user 
LEFT JOIN items ON user.us_id = items.us_id 
LEFT JOIN orders ON items.od_id = orders.od_id 
WHERE user.us_id = $usrID
 
Share this answer
 
SQL
attach
ON DELETE CASCADE 
to all your foreign key contraints...

Let EmployeeTable is parent table and EmpID is PK on this.

So when you create other tables to store employee details you may create relationships too... The relationship created with the above statement will do the trick. So when you delete EmpID from parent table, it will be deleted from all children tables.

SQL
ALTER TABLE EmployeeSal
ADD CONSTRAINT FK_EmplID
FOREIGN KEY (EmpID)
REFERENCES EmployeeTable (EmpID)
ON DELETE CASCADE;


http://msdn.microsoft.com/en-us/library/aa933119(v=sql.80).aspx

Thanks,

Kuthuparakkal
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900