且构网

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

如何删除具有列作为SQL Server中其他表的外键的表中的行?

更新时间:2023-01-29 08:50:50

使其变得困难的全部原因在于引用完整性.如果从父表中删除该行,则将修改其他表中的所有行,这将导致查询等问题.在关系上设置ON DELETE CASCADE也可以删除子表中的行.

http://msdn.microsoft.com/en-us/library/aa933119 (v = sql.80).aspx [
The whole point in making it difficult is because of referential entegrity. If you delete the row from the parent table then all rows in any other table would be ophaned and would cause problems with queries and such. Set ON DELETE CASCADE on the relationship to delete the rows in the child table as well.

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


ALTER TABLE MasterTable
ADD CONSTRAINT fk_xyz 
FOREIGN KEY (xyz) 
REFERENCES ChildTable (xyz) ON DELETE CASCADE


首先,您必须从子表中删除记录,然后从父表中删除.
First u have to delete the record from Child table then remove from parent table .