且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-05 13:50:02

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