问题描述
我有一个员工表和与员工相关的表.如果我从表中删除一名雇员,则应从所有其他表中删除与该雇员有关的所有数据
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
推荐答案
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 =
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
这篇关于我有一个员工表和与员工相关的表.如果我从表中删除一名员工,则应从所有其他表中删除与该员工相关的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!