SQL Server trigger delete or cascade delete

时光毁灭记忆、已成空白 提交于 2019-12-11 18:24:23

问题


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.

I've been busy looking on stackoverflow for a solution but (being new) it does not make any sense to me.

I'm using SQL Server 2012.

I have the following relationship (with foreign key constraints in place)

Client > Order > OrderItems

Order

Id
ClientId

OrderItems

Id
OrderId

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

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?


回答1:


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



来源:https://stackoverflow.com/questions/12280466/sql-server-trigger-delete-or-cascade-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!