SQL Server触发器删除或级联删除 [英] SQL Server trigger delete or cascade delete
问题描述
首先,除了基本的CRUD之外,我还没有做太多的SQL代码,但是我参与了一个项目,在这个项目中,我可以访问SQL Server并由我自己来编写SQL.
First off I haven't done much SQL code before only the basic CRUD, but I'm involved in a project in which I have access to SQL Server and its up to me to write the SQL.
我一直在忙着寻找stackoverflow的解决方案,但是(是新的)对我来说没有任何意义.
I've been busy looking on stackoverflow for a solution but (being new) it does not make any sense to me.
我正在使用SQL Server 2012.
I'm using SQL Server 2012.
我有以下关系(已设置外键约束)
I have the following relationship (with foreign key constraints in place)
Client > Order > OrderItems
订购
Id
ClientId
OrderItems
Id
OrderId
我正在使用EF,当我在客户端上调用delete方法时,我需要删除 orders
和 orderitems
表中的所有相关项
I'm using EF and when I call my delete method on client I need to delete all the the related items in orders
and orderitems
tables
我需要添加一个触发器来删除 orders
和 orderitems
,但是我不确定如何执行此操作,或者不确定是否要进行级联删除(我已经听说过)最好?
I need to add a trigger to go and delete orders
and orderitems
, but I'm not sure how to do this or if a cascade delete (I've heard of) is best?
每个人都有一个简单的示例和建议,如何做到这一点?
Anyone have a quick example and advice of how to do this?
推荐答案
在简单情况下,请使用级联删除.
For simple situations, use the cascade delete.
如果您有更复杂的要求,请使用触发器或存储过程进行删除
If you have more complex requirements, use the triggers, or a stored procedure for your deletions
http://msdn.microsoft.com/en-us/library/aa902684(v=sql.80).aspx
这篇关于SQL Server触发器删除或级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!