我有一个员工表和与员工相关的表.如果我从表中删除一名员工,则应从所有其他表中删除与该员工相关的所有数据 [英] 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

查看:80
本文介绍了我有一个员工表和与员工相关的表.如果我从表中删除一名员工,则应从所有其他表中删除与该员工相关的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个员工表和与员工相关的表.如果我从表中删除一名雇员,则应从所有其他表中删除与该雇员有关的所有数据

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

推荐答案

您的问题是什么?操作还是无法做到?可以尝试一下.
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


attach
ON DELETE CASCADE 

至您所有的外键约束...

设EmployeeTable为父表,EmpID为PK.

因此,当您创建其他表来存储员工详细信息时,您也可以创建关系.因此,当您从父表中删除EmpID时,它将从所有子表中删除.

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.

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

谢谢,

Kuthuparakkal



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

Thanks,

Kuthuparakkal


这篇关于我有一个员工表和与员工相关的表.如果我从表中删除一名员工,则应从所有其他表中删除与该员工相关的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆